(Noobie) Why am I receiving this "Object of NoneType has no len()' error [duplicate]
Solution 1:
list.append
is a method that modifies the existing list. It doesn't return a new list -- it returns None
, like most methods that modify the list. Simply do aList.append('e')
and your list will get the element appended.
Solution 2:
Generally, what you want is the accepted answer. But if you want the behavior of overriding the value and creating a new list (which is reasonable in some cases^), what you could do instead is use the "splat operator", also known as list unpacking:
aList = [*aList, 'e']
#: ['a', 'b', 'c', 'd', 'e']
Or, if you need to support python 2, use the +
operator:
aList = aList + ['e']
#: ['a', 'b', 'c', 'd', 'e']
^ There are many cases where you want to avoid the side effects of mutating with .append()
. For one, imagine you want to append something to a list you've taken as a function argument. Whoever is using the function probably doesn't expect that the list they provided is going to be changed. Using something like this keeps your function "pure" without "side effects".
Solution 3:
Delete your second line aList = aList.append('e')
and use only aList.append("e")
, this should get rid of that problem.