Check if List is Empty in Python (3 Examples)
Welcome again to another interesting Python programming tutorial. In this one, you’ll learn 3 methods of checking if a list is empty in Python.
Here is a glimpse of the 3 methods:
Let’s get coding!
Example 1: Using len() Function
First, we will create a simple Python list object of different car brands:
carlist = ["ford","sedan","toyota","mercedes","volkswagen","honda"]
Next, we will use the Python len()
function to check if the list is empty or not:
if len(carlist) > 0: print("Is not empty") else: print("Is empty") # "Is not empty"
If the length of the list is greater than 0, that means the list is not empty, in which case the program will print “Is not empty”. But if the length of the list is not greater than 0, then it means the list is empty.
Example 2: Using “not” Operator
In this example, we will use Python’s “not” operator to check if the list is empty or not:
if not carlist: print("Is empty") else: print("Is not empty") # "Is not empty"
Let us demonstrate it with an empty list:
carlist = []
if not carlist: print("Is empty") else: print("Is not empty") # "Is empty"
Applying the “not” operator on an empty list returns “Is empty”.
Example 3: Using Empty Parenthesis [ ]
In this example, we will use an empty parenthesis to check if the list is empty or not:
carlist = ["ford","sedan","toyota","mercedes","volkswagen","honda"]
if carlist == []: print("Is empty") else: print("Is not empty") # "Is not empty"
So, any of the three methods above can be used to check if a list is empty or not in Python.
Video, Further Resources & Summary
Do you need more explanations on how to check if a list is empty in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to check if a list is empty in Python.
The YouTube video will be added soon.
Furthermore, you should check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Count Duplicates in List in Python (2 Examples)
- Convert List from Character to Integer in Python (Example)
- Using Lists in Python (Introduction)
- Sort List of datetime Objects in Python (2 Examples)
- Access List Element by Index in Python (Example)
- Learn Python Programming
This post has shown how to check if a list is empty in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter