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.

FAQs for the Codecademy Python exercise Using a list of lists in a function:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

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

Need broader help or resources about Python in general? Go here!

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

Learn more about how to use this guide.

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

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • 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 (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

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

but think about it, is your function ready to hand back data after just doing a single appending to your results list?

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)

comparing it to your 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)

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

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]]

Add your function here

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 :slight_smile:

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