18. Why is my code only returning one item?



Oops, try again. flatten([[1, 2], [3, 4]]) returned [1] instead of [1, 2, 3, 4]

Why is my code returning only one item on the list instead of the entire list?

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

def flatten(lists):
  results = []
  for numbers in lists:
      for item in numbers:
          return results

print flatten(n)


Bring the indent for return results into line with the initial for.


Hi @rubyrockstar16575 ,

You have this statement indented to a degree that makes it part of the inner for loop, so it returns the results list right after it has appended the first item to the list ...

return results


That did it. Thanks!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.