Using a list of lists in a function error, help


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/4/2?curriculum_id=4f89dab3d788890003000096#


It's saying that flatten([[1, 2], [3, 4]]) returned [[[1, 2], [3, 4]], [[1, 2], [3, 4]], [[1, 2], [3, 4]], [[1, 2], [3, 4]]] instead of [1, 2, 3, 4]. I am not sure what I did wrong as I did everything they've asked, please help me.


Here is my code:


Replace this line with your code. 
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

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




print flatten(n)


#2

Here:

It should be:

for lists in numbers:

which you later append to results here:

I hope this helps! :slight_smile:


#3

Thank you so much. This really helped, it was a small error I could not find. :relaxed:


#4

Yeah... It does happen sometimes. With more practice, we get perfect.

Glad I could help! :slight_smile:


#5

Ok this is boggling my mind. It's like list inception. I am stuck on this one too. Can anyone explain to me like I'm 5 the multiple 'for' statements within each other? I put my code that doesn't work below. I am getting the following error:

Traceback (most recent call last):
  File "python", line 12, in <module>
  File "python", line 7, in flatten  <---Line 7 is where I have "results = results + numbers[lists]"
TypeError: can only concatenate list (not "int") to list

def flatten(lists):
    results = []
    for numbers in lists:
        for lists in numbers:
            results = results + numbers[lists]
    return results

#6

Hi!

The fifth instruction:

For each number, .append() it to results

says to do:

results.append(lists)

for this line:

I think this should resolve.


#7

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