# FAQ: Learn Python - Lists and Functions - Using a list of lists in a function

This community-built FAQ covers the “Using a list of lists in a function” exercise in Codecademy’s lessons on Python.

## Join the Discussion. We Want to Hear From You!

Agree with a comment or answer? Like () to up-vote the contribution!

Want to take the conversation in a totally different direction? Join our wider discussions.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

## Other FAQs

• This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
• This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try () in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post ().

2 posts were split to a new topic: Confused

The result of following code shows None instead of [1, 2, 3, 4, 5, 6,7,8,9]
Can anyone help me identify the problem in my code:

``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):

results = [ ]

for lst in lists:

for numbers in lst:

return results.append(numbers)

print flatten(n)
``````

`.append()` appends to the list in memory, as such, it doesn’t have a return value. So you get None (the absence of a return value

Sorry but I have just started learning this language and this is my first language which I am learning. When I did not get the desired result I look at its solution and it was quite the same as I have written it. Can you please tell me specifically what is wrong with it. I will appreciate it a lot. Thanks

looking at the solution of the FAQ:

``````list_of_lists = [ [1, 2, 3], [4, 5, 6] ]

def reset_list_items(nested_list):
zeros_result = []
for each_list in nested_list:
for item in each_list:
zeros_result.append(0)
return zeros_result

print reset_list_items(list_of_lists)
``````

``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):

results = [ ]

for lst in lists:

for numbers in lst:

return results.append(numbers)

print flatten(n)
``````

But the focus shouldn’t sole be on the difference, you need to be able to explain them in plain English

the solution loops over the nested lists (with nested loops), appending to a list to flatten the data. Then after the loops, return the flattened list.

your code appends the first value it find to the results list, given a return keyword is reached, the function hands back data and is done executing.

The FAQ which is mentioned in this topic:

How do nested loops work?

also has a very detailed explanation?

1 Like

Thanks a lot. I had no idea that for every for loop a 0 will be returned as a result.

that depends what you append to the list.

1 Like

In general, could one use List Comprehension for completing this exercise? Just curious.

Yes, this is possible. The question though is if you should, doesn’t make the code very readable.

Hello! I understand the correct answer, however, I tried the code like this:

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

def flatten(lists):
results = 0
for numbers in lists:
for num in numbers:
results = results + lists.apppend(num)
return results

print flatten(n)
``````

and I don’t understand why `lists.append(num)` is not adding each value of the list into the loop… Can someone explain me that? Thanks!

you made a typo in `.append()`

also, this line:

``````results = results + lists.apppend(num)
``````

what are you doing here? So many operations going on, just confusing.

okay, maybe the better question: What do you attempt to do?

With `results = results + lists.apppend(num)` I’m attempting to get the first value of the `n`, store it, and in the next round, append it to the next value…

So what do you need to end up with? a list, right? Why is result an integer then? And why would you append to `lists`?

but `.append()` its a value to the list in memory, the method doesn’t return anything, so that will result in `None`. So then you integer + None, which results in an TypeError or something like that

1 Like

Hello there. I’m confused here. could anyone help me understand how my code is not working.

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

def flatten(lists):

results =

for numbers in lists:

``````for number in numbers:

results = results.append(number)

return results
``````

print flatten(n)

`return` means literally that: returning/handing back data to the caller/function call. Which signals that the function is done executing

Hi! I am a little lost on why my code is not working. Would you be able to help me?

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

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

the reply of which you quote already explains the problem you have when using `return` in the loop?

I was loosing my hair on this exercise.

This worked for me

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

Pay careful attention to the indendation on your code.

Hope this helps