Append function is evaluating the empty list beforehand, how do I get past this?


#1


Append function is evaluating the empty list beforehand, how do I get past this ?


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


I've defined an empty list and append is evaluating it as none prior to appending it
Traceback (most recent call last):
File "python", line 9, in
File "python", line 7, in flatten
AttributeError: 'NoneType' object has no attribute 'append'


It was supposed to append into the list


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)

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


#2

It may work if you use results.append(number) instead of results= results.append(number). My guess is that with results = results.append() you are defining something new with Type "None" . But dont take this to serious it is just a guess. Please correct me if necessary


#3

@mkay_93 The .append() method directly alters the list it's attached to. So you're right, there's no need to then store the changed list to a variable. I don't know specifically what is meant by the NoneType error, but the = assignment is definitely redundant.


#4

Btw you're right, he just needed to remove his "results =" part of "results = results.append(number)"


#5

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