18. Why is my code only returning one item?


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/4/2?curriculum_id=4f89dab3d788890003000096#


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:
          results.append(item)
          return results




print flatten(n)


#2

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


#3

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

#4

That did it. Thanks!


#5

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