# Using a list of lists in a function

#1

The exercise requests "For each number, .append() it to results" but when this is used the results keep the lists separate. Here are the result

``````[[1, 2, 3], [4, 5, 6, 7, 8, 9]]
None``````

``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for numbers in lists:
results.append(numbers)
return results

print flatten(n)``````

Now I understand that the code below achieves the desired output, however it is clear I am not understanding why the .append() is behaving the way it does above. If anyone would be gracious enough to explain this to me. I would greatly appreciate it.

``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for numbers in lists:
results += numbers
return results

print flatten(n)``````

is it just that the .append() operation will recognize separated lists and therefore cannot be used to concatenate the two lists together in this example?

#2

well, `numbers` is a list, so append will simply append the list to results

to solve this exercise, you need two loops, the second one to get the values from the inner list which you can then append to results

#3

so does the code below create a single list because the 'results += numbers' is creating a new list by just looking at the values in 'n'?

``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for numbers in lists:
results += numbers
return results

print flatten(n)``````

#4

because `+=` will extend the list, rather then appending

#5

I see so if we are using two loops it would look something like this.

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

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

print flatten(n)``````

I was having trouble considering the lists as their own items with in a list. There was a disconnect with the concept of an outer list and then the inner lists within. Thank you for clearing it up. I appreciate it.

#6

exactly, seems you got it all sorted now

#7

So you brought up a good point of appending versus extending. Is it right to think that extending using the '+=" operator is the easier way to concatenate two lists together when using a loop like this?

#8

no idea, if you really want to know google how you can measure execution time of python scripts

#9

Okay will do. Thank you.

#10