Appending list, but the 'NoneType' object does not have the 'append' attribute

I have a script in which I retrieve the value for each user and add it to the list, but I get the object "NoneType" does not have the attribute "add". My code is like

last_list=[] if p.last_name==None or p.last_name=="": pass last_list=last_list.append(p.last_name) print last_list 

I want to add the last name to the list. If not, do not add it to the list. Please help. Note: p is an object that I use to receive information from my module, which has all first_name, last_name, age, etc. .... Please suggest .... Thanks in advance

+7
source share
3 answers

When you execute pan_list.append(p.last) you perform the inplace operation, this is an operation that modifies the object and returns nothing (i.e. None ).

You should do something like this:

 last_list=[] if p.last_name==None or p.last_name=="": pass last_list.append(p.last) # Here I modify the last_list, no affectation print last_list 
+12
source

list modified

Change

 last_list=last_list.append(p.last_name) 

to

 last_list.append(p.last_name) 

will work

+6
source

I think you want:

 last_list=[] if p.last_name != None and p.last_name != "": last_list.append(p.last_name) print last_list 

Your current if statement:

 if p.last_name == None or p.last_name == "": pass 

Effectively never does anything. If p.last_name is not a single or empty string, it does nothing inside the loop. If p.last_name is something else, the body of the if statement is skipped.

Also, it looks like your pan_list.append(p.last) is a typo, because I don't see either pan_list or p.last used anywhere in the code you sent.

+2
source

All Articles