Profile image of BoomBox
Submitted by BoomBox
over 11 years

18/18 AttributeError: 'NoneType' object has no attribute 'append'

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


def flatten(lists):
    results = []
    for i in range(0,len(lists)):
        results = results.append(lists[i])
    return results

print flatten(n)

Traceback (most recent call last): File “python”, line 10, in

Anyone know what is wrong?

Answer 531f073352f863ed72002f0f

3 votes

Permalink

I got this to work:

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

print flatten(n)
Profile image of systemSurfer29094
Submitted by systemSurfer29094
over 11 years

1 comments

Profile image of digitalSlayer22298
Submitted by digitalSlayer22298
over 11 years

thankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyouthankyou

Answer 531f639280ff33cd91004dce

1 vote

Permalink

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


def flatten(lists):
    results = []
    for i in range(0,len(lists)):
        results = results.append(lists[i])
    return results

print flatten(n)

This way you are basically trying to append lists inside results.

Variable n is a list of lists! Which means when you iterate through n it will simply return more lists! To get elements you need to iterate inside the list which is already located inside an list.

Profile image of linaran
Submitted by linaran
over 11 years

1 comments

Profile image of Choki_Wukong
Submitted by Choki_Wukong
over 11 years

For some reason, I can’t get the range method to work for this lesson. I feel like I did the same thing as above but I used “x” instead of “i” for the variable name in the for loop.

Answer 531fc23e548c35d81a004bce

0 votes

Permalink

Really?

Profile image of cybershats
Submitted by cybershats
over 11 years

Answer 531fdaa6548c35cbc10073cc

0 votes

Permalink

Ah i see, so it’s like n[1] = [ 1, 2, 3] n[2] = [ 4, 5, 6, 7, 8, 9]

Okay, I think I get it now.

Profile image of BoomBox
Submitted by BoomBox
over 11 years

2 comments

Profile image of linaran
Submitted by linaran
over 11 years

It’s exactly like that! => if you want to know how is it possible check out pointers in C,

Profile image of easternlai
Submitted by easternlai
over 11 years

me too :(

Answer 55ed3d5786f55274b700056e

0 votes

Permalink

This is the Correct answer.

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

def flatten(lists):
 results = [ ]
 for numbers in lists:
    print numbers
    for i in numbers:
        print i
        results.append(i)
 return results
print flatten(n)
    
Profile image of bhartirawat
Submitted by bhartirawat
almost 10 years