What is NoneType in Python and Null Equivalent?

What is NoneType in Python and Null Equivalent?

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.

When is NoneType used?

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):

  • If the Python regular expression in re.search does not match, it returns NoneType object.
  • When you search key in the Python dictionary and the key is not present, it returns NoneType object.
  • The None keyword is also used for matching or identifying if a certain function returns any value or not.

What is Equivalent to NULL in Python?

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.

How to filter out None values from List and Dictionary?

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.

6 Comments

  1. 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: –

    An error has occurred while executing Python code: 
    
    AttributeError: 'NoneType' object has no attribute 'startswith' 
    Traceback (most recent call last):
    
  2. elevation = soup.find('div', {'id':'elevation'})
        altitude = elevation.find('span', {'class': "value"})
    

    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?

    1. 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’.

Leave a Reply

Your email address will not be published. Required fields are marked *