Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

appending list but error 'NoneType' object has no attribute 'append' [duplicate]

Tags:

python

list

I have a script in which I am extracting value for every user and adding that in a list but I am getting "'NoneType' object has no attribute 'append'". 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 last name in list. If its none then dont add it in list . Please help Note:p is the object that I am using to get info from my module which have all first_name ,last_name , age etc.... Please suggest ....Thanks in advance

like image 202
learner Avatar asked Oct 15 '12 11:10

learner


People also ask

How do I fix NoneType has no attribute?

The Python "AttributeError: 'NoneType' object has no attribute 'get'" occurs when we try to call the get() method on a None value, e.g. assignment from function that doesn't return anything. To solve the error, make sure to only call get() on dict objects.

How do I fix AttributeError NoneType object has no attribute text?

Double-check the web pages that you will be scraping to get information. Make sure that all element exists. Verify that the selector or selectors you are using are correct.

How do I fix NoneType in Python?

The error “TypeError: 'NoneType' object is not iterable” occurs when you try to iterate over a NoneType object. Objects like list, tuple, and string are iterables, but not None. To solve this error, ensure you assign any values you want to iterate over to an iterable object.

Why does list append return None in Python?

append() is an in-place operation, meaning that it modifies the state of the list , instead of returning a new list object. All functions in Python return None unless they explicitly return something else. The method a. append() modifies a itself, which means that there's nothing to return.


2 Answers

list is mutable

Change

last_list=last_list.append(p.last_name) 

to

last_list.append(p.last_name) 

will work

like image 131
jessiejcjsjz Avatar answered Oct 07 '22 05:10

jessiejcjsjz


When doing pan_list.append(p.last) you're doing an inplace operation, that 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 
like image 24
Cédric Julien Avatar answered Oct 07 '22 07:10

Cédric Julien