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
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.
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.
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.
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.
list is mutable
Change
last_list=last_list.append(p.last_name)
to
last_list.append(p.last_name)
will work
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With