Appending turns my list to NoneType [duplicate]
Asked Answered
P

4

38

In Python Shell, I entered:

aList = ['a', 'b', 'c', 'd']  
for i in aList:  
    print(i)

and got

a  
b  
c  
d  

but when I tried:

aList = ['a', 'b', 'c', 'd']  
aList = aList.append('e')  
for i in aList:  
    print(i) 

and got

Traceback (most recent call last):  
  File "<pyshell#22>", line 1, in <module>  
    for i in aList:  
TypeError: 'NoneType' object is not iterable  

Does anyone know what's going on? How can I fix/get around it?

Pointless answered 1/10, 2010 at 15:45 Comment(0)
S
59

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.

Smaragd answered 1/10, 2010 at 15:46 Comment(6)
And since it doesn't return anything, you're setting aList to None if you do do the assignment, which is why you're getting the error.Detailed
All functions return something :)Smaragd
true - but sometimes that something is None. But is None really something? My head hurts!Reopen
@kindall: "doesn't return anything" should be "in effect, it's the same is if the method doesn't have a return statement and returns None implicitly". And. "Methods which mutate an object almost never return a value, pop is the notable exception."Imprimis
the answer "list.append return None" is saved my day. I was just use aList = aList.append('e') and wonder why it's return None. Now i know the reason why, all i have to do is use aList.append('e')Invocation
@ThomasWouters using aList still keeps the list being called 'list', and doesn't make a new list?Brachium
B
5

Delete your second line aList = aList.append('e') and use only aList.append("e"), this should get rid of that problem.

Beseech answered 14/1, 2015 at 21:12 Comment(0)
V
5

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".

Ventage answered 26/2, 2018 at 2:32 Comment(0)
K
0

Sometimes this error appears when you forgot to return a function at the end of another function and passed an empty list, interpreted as NoneType.

from this:

def func1():
  ...
  func2(empty_list)

def func2(list):
  ...
  # use list here but it interpreted as NoneType

to this:

def func1():
  ...
  return func2(empty_list)
    
def func2(list):
  ...
  # use list here, it will be interpreted as an empty List
Kampmeier answered 14/7, 2021 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.