Purify "NoneType" object has no attribute 'append'


#1

Need some help in this code

def purify(numbers):
    new_numbers = []
    for number in numbers:
        if number % 2 == 0:
            new_numbers = new_number.append(number)
    return new_numbers

Some questions :slight_smile:
How to create an empty list.
and what is NoneType & why is it showing in my code.
Thanks


#2

you did this? you created an empty list here:

new_numbers = []

the problem is here:

new_numbers = new_number.append(number)

append() will update the list for you, no need to store it in a variable as well

longer answer, because append() updates the list for you (manipulating the list directly) it returns None (the default), so by storing the returned result of append() in a variable, you actually store None new_numbers, overwriting the list, so then the second run of the loop, you try to append to None, that doesn't work (see the error message)


#3

Thanks for the good and detailed answer!


#4