Using a list of lists in a function error, help


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:
    return results

print flatten(n)



It should be:

for lists in numbers:

which you later append to results here:

I hope this helps! :slight_smile:


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


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

Glad I could help! :slight_smile:


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



The fifth instruction:

For each number, .append() it to results

says to do:


for this line:

I think this should resolve.


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