Table of Contents
In Python,
Check if Variable Is None
andCheck if Variable Is null
have same solutions as meaning of both queries is same.
1. Introduction
In Python Programming, checking when a variable is None(Python equivalent of null or nil in other languages) is a common task, particularly in functions returning no value, handling optional parameters etc.
In this article, we will see different ways to check if variable is None in Python.
2. What is None in Python?
In Python, None
is a special constant that denotes the absence of value or a null value. It is object of its own datatype, the NoneType.
3. Using the is Operator
The is
operator is the most straightforward and recommended method to check if a variable is None.
1 2 3 4 5 |
x = None if(x is None): print("x is of the 'None' type.") |
Output:
The is
operator checks whether two references point to the same object. Since None is a singleton in Python, all instances of None
point to the same memory location.
This is very efficient due to direct memory address comparison. It is standard pythonic way to check if variable is None in Python.
4. Using Equality Operator ==
The equality operator ==
is another way to check if variable is None in Python, but it is not recommended.
1 2 3 4 5 |
x = None if(x == None): print("x is of the 'None' type.") |
Output:
The ==
operator checks whether two objects are equals or not. In Python, None
equals None
.
However, this method is not recommended because some custom objects might override the __eq__
method, leading to incorrect results while comparing with None
.
This method may be slower than is
operator because it might involve complex comparisons depending on object’s __eq__
implementation.
Further reading:
5. Checking None in a Conditional Expression
Inline None
checks are used while filtering data from list, dictionaries, set.
Let’s see with help of example:
1 2 3 4 5 6 7 8 |
my_var = None result = "No Value" if my_var is None else "Has Value" #Output: No Value # Using in list comprehensions my_list = [1, None, 3, None] none_filtered = [x for x in my_list if x is not None] #Output: [1, 3] |
It allows more readable and concise None
checks rather than if-else
block.
6. Handling Collections Containing None
In Python, collections like lists, dictionaries may contain None
element and we might need to filter them to cleanup the data.
Let’s see how to filter None
from list and dictionaries.
6.1 Filtering from the List
Let’s use list comprehension to filter None
from list.
1 2 3 4 |
my_list = [1, None, 3, None, 5] filtered_list = [x for x in my_list if x is not None] #Output: [1, 3, 5] |
We can also use filter()
function to filter the None
, and convert it back to list using list()
function.
1 2 3 4 |
my_list = [1, None, 3, None, 5] filtered_list = list(filter(None, my_list)) #Output: [1, 3, 5] |
6.2 Filtering from the Dictionary
While working with dictionaries, key and value both can be None
.
Let’s filter None
value from the Dictionary:
1 2 3 4 5 6 7 8 |
my_dict = {"a": 1, "b": None, "c": 3, "d": None} filtered_items = filter(lambda item: item[1] is not None, my_dict.items()) filtered_dict = dict(filtered_items) print(filtered_dict) # Output: {'a': 1, 'c': 3} |
filter()
applies the lambda function to each item (key-value pair) inmy_dict.items()
.- The
lambda
function returns True if the value (accessed by item[1]) is not None. dict(filtered_items)
converts the filtered pairs back into a dictionary.
7. Conclusion
In this tutorial, we have discussed various method to check if variable is None
in Python. We also discussed about how to filter None
from collections like list, dictionaries etc.
The is
operator is the most direct and preferred method due to its efficiency and accuracy, making it suitable for most scenarios.