NoneType
in Python is the data type of the object when the object does not have any value. You can initiate the NoneType
object using keyword None
as follows.
obj = None
Let’s check the type of object variable ‘obj’.
obj = None
type(obj)
Output:
<type 'NoneType'>
NoneType
object indicates no value.
If you try to print the value of the NoneType
object, it does not print anything on the Python interpreter console.
It is useful in many places.
It is the default return type of the function when the function does not return any value.
Use cases (Examples):
NoneType
object.None
keyword is also used for matching or identifying if a certain function returns any value or not.You might have seen the NULL value in many of the programming languages but Python. In, PHP, the null value is represented as NULL
. In Java programming, it is represented with the keyword Null
.
In Python, there is no keyword NULL. Here None keyword is used as equivalent to the NULL.
If you are moving from other programming languages to Python, don’t get confused with None
keyword or NoneType
object in Python.
Remove None Elements from List:
If you want to remove all the None type elements from the Python list, use the filter and lambda function in Python.
list_in = [1, 3, 'cse', None]
lambda_obj = lambda x: (x is not None)
list_out = list(filter(lambda_obj, list_in))
print(list_out)
Output:
[1, 3, 'cse']
Remove Dictionary Entries having Value None:
If you want to remove entries from the dictionary that has values None, refer this tutorial, Or, here is the simple code.
dic_in = {'a': 2, 'b': 0, 'c': 0, 'd': 4, 'e': None}
dic_out = {x:y for x,y in dic_in.items() if y is not None}
print(dic_out)
Output:
{'a': 2, 'b': 0, 'c': 0, 'd': 4}
Hope this helps you to understand lot of concepts related to the None type and equivalent Null type in Python.
For more about Python, do check the complete Python tutorial.
I am using RiverGIS under QGIS and it works on python code: –
while executing Creating RAS GIS Import file from HEC-RAS model geometry…
I am getting this error code: –
Abhimanyu, you are using
startswith()
method with the NoneType object.For example
Here, str is a NoneType object. It should be a string.
I am using altitude data by requesting the website through selenium web driver in anaconda3. For the line, altitue=elevation.find, it says ‘NoneType’ object has no attribute ‘find’.
Can you help me out?
Hi Pradhan,
From code what I can see is that soup.find() is returning none. One of the reasons behind could be ‘soup’ does not have ‘div’ having ‘id’ as ‘elevation’.
You can print the ‘soup’ and check the text if you have that ‘div’.
Thank you so much for this; I was stuck for about an hour before I found this.
You’re welcome. And I’m glad you find this guide useful.