I know I can use "append()" to add an element to a list, but why does the assignment return None?
>>> a=[1,2,3]
>>> a.append(4)
>>> print a
[1, 2, 3, 4]
>>> a=a.append(5)
>>> print a
None
>>>
Because you're assigning an output to the act of appending which has no output, so instead of:
a=a.append(5)
you just want
a.append(5)
I try to explain in the easiest way: With:
a.append(5)
you are calling the function that adds something (in this case 5) to a
With:
a = a.append(5)
you are saying that a is equal the result of the funcion .append(). But append just modifies an existing list, it does not returns anything, and it does not, as you thought, return a new list with the element appended.
This just indicates that the "append" method does not return anything. It only modifies the existing list. It does not return an updated list.
Don't bother doing a = a.append(4)
. If you just want to add an entry to the list, do a.append(4)
to modify a
.