18. Using a list of lists in a function


#1




My error message: 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 x in numbers:
            results.append(x)
    return results



print flatten(n)


#2

look at this code:

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
   print lists

# function call
print flatten(n)

see how we pass the argument n at the function call? so now n is stored into the function parameter lists, so if you now where to loop over lists instead of n, your function would work for more then one list

currently your function works for only one lists, bit limited.


#3

I get it, but how can I make my function work for more than one list?


#4

^?

the trick is in the arguments and parameter. I gave a pretty big clue about it?


#5

I changed it to this and now it works:

for numbers in lists:
for x in numbers:


#6