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"
my_list is not None
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.
Method #2 Using "!= None"
In the second method, we'll use the not equal operation to check if a list is None/null.
my_list != None
my_list = [1, 2, 3, 4] print(my_list != None)
Method #3 Using "mylist:"
my_list = [1, 2, 3, 4] print(True if my_list else False)
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.