Last modified: Jan 10, 2023 By Alexander Williams
How Properly Check if a List is not Null in Python
In this tutorial, we're going to learn three methods to check if a python list is empty.
But first, we need to know that in python we use None instead of null.
Method #1 Using "is not None"
syntax:
my_list is not None
example:
my_list = [1, 2, 3, 4]
print(my_list is not None)
As you can see in the above code, we've checked my_list, and if the list is None, we'll get True in output.
output:
True
Method #2 Using "!= None"
In the second method, we'll use the not equal operation to check if a list is None/null.
syntax:
my_list != None
example:
my_list = [1, 2, 3, 4]
print(my_list != None)
output:
True
Method #3 Using "mylist:"
syntax:
mylist:
example:
my_list = [1, 2, 3, 4]
print(True if my_list else False)
output:
True
4. Conclusion
Today we've learned three different methods to check if a list is null in python.
Probably now you asked yourself, what is the good method?
Answer: method #1 because it's more accurate than the others.