18. using a list of lists in a function


#1



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


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


what am i missing?


Replace this line with your code. 
def flatten(lists):
    results = []
    for numbers in lists:
        for number in numbers:
            results.append(numbers)
        return results
print flatten(n)


#2

why do you append numbers?

a function ends the moment a return keyword is reached, so your first for loop makes only one iteration


#3

typo,

def flatten(lists):
    results = []
    for numbers in lists:
        for number in numbers:
            results.append(number)
            return results
print flatten(n)

#4

now your function ends really early

you want to return results after all your loop have finished running


#5

how do i/you know my loops ends early, im lost as to how that is shown

thank you


#6

because when a return keyword is reached, the function ends (so the loop(s) will break)

You should see by the output that not enough is appended to your list?


#7

i do see that not enough is there. so am i "return results" too soon? i tried indenting and putting on new line but that puts it outside of the loop


#8

it should be outside the loops and inside the function? Which requires good indention.


#9

def flatten(lists):
results = []
for numbers in lists:
for number in numbers:
results.append(number)
return results
print flatten(n)

i was paying attention to the wrong loop thanks man!


#10

yea, you have to keep both loops in mind


#11

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