Profile image of BoomBox
Submitted by BoomBox
almost 12 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
almost 12 years

1 comments

Profile image of digitalSlayer22298
Submitted by digitalSlayer22298
almost 12 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
almost 12 years

1 comments

Profile image of Choki_Wukong
Submitted by Choki_Wukong
almost 12 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
almost 12 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
almost 12 years

2 comments

Profile image of linaran
Submitted by linaran
almost 12 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
almost 12 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
over 10 years

Popular free courses

  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      4 Lessons
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      11 Lessons
  • Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
    • Beginner Friendly.
      6 Lessons
Explore full catalog