18 using a list of lists in a function


#1



I get this error when I try to run this code, but I can't see what I do wrong.

Oops, try again. flatten([[1, 2], [3, 4]]) returned [1, 2, 3, 4, 5, 6, 7, 8, 9] instead of [1, 2, 3, 4]


n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results = []
    for numbers in n:
        for i in numbers:
            results.append(i)
    return results



print flatten(n)


#2

You only need one for loop and it should loop through your parameter lists because the codeyou have is not looping through anything.

for i in lists: #your argument (lists)

not,

Also you should append everything to your loop variable for example if it is i you would write,

results += i


#3

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