There are several ways to check if a list is empty. The first one covered below,
if list returns as false, is the most
pythonic. In other words, it is the one people recommend the most in Python. The other strategies we will discuss are semantically correct (meaning they compile and run) but are not considered good form.
In this post, we’ll look into how to check if a list is empty in Python. We will go over several ways to check for a list:
- checking if the list value returns as true or false
- using len()
- comparing your list to an empty list
First, let’s quickly go over what lists are.
What Are Python Lists?
Python lists are ways to store various items together. For example, if I want to group my grocery list items into one variable, I would list a list instead of writing one variable per grocery item. Declaring all of these related items in one list saves me time since I don’t have to declare multiple variables.
grocerylist = ["eggs", "fruits", "kale", "grapefruits"]
That way, when I want to see my grocery list, I can use a single print statement:
print(grocerylist) # ['eggs', 'fruits', 'kale', 'grapefruits']
The list does not have to contain only strings like in the above example. It could contain other values such as booleans (true or false) or numbers, or even a combination of these.
Python Check if List is Empty: False Values
In Python, sequences such as strings, tuples, and lists return as false if they are empty and as true if they’re not empty.
The value of any object, including a list, can be evaluated as a boolean value, either true or false, and this value will be returned to you. In the case of list objects, all have a value of true unless they are empty. Knowing this value can be helpful in conditional statements such as the
if statements below. Based on the result returned (which in this case lets you know if a list is completely empty), you can decide what action to take.
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
ourList =[ ]
Check if our list does not return true:
if not ourList: print("The list is empty") #This will return: The list is empty
Alternatively, you can check if the list is true. If the list is not empty, then you know that you carry out actions like printing the list and values will be shown on the screen!
if ourList: print("The list is not empty") #Since ourList is empty this line will not print-- nothing will print to the screen
Stating the name of our list alone along with
if will make Python evaluate if the list is true or false depending on whether the list has items or is empty.
You can use the length function, len(), to check for the length of the list. By extension, you’d be checking for its emptiness. According to the Python style guide it is not recommended that you use len() to check for emptiness. This is because you can just check using its inherent boolean value, which is a more elegant and direct option. Using len() actually requires more checking by Python behind the scenes.
ourList =[ ]
if statement is asking if ourList has a length value. If so, it will print our statement:
if len(ourList): print("The list is not empty") #Since ourList is empty, this line will not print-- nothing will print to the screen
if statement will ask if ourList does not have a length value. If so, it will print our statement:
if not len(ourList): print("The list is empty") #The list is empty
Another way to use leng() is to compare the result of length to “0”.
if len(ourList) == 0: print("The list is empty") #The list is empty
Comparing Against an Empty List
We can also compare our list against an empty list ([ ]). You may want to do this if you want to make visibly clear what you’re comparing against. This method of comparison is also not the recommended option in Python, although it would be semantically correct. Python has to build an unnecessary list (the [ ] you are comparing yours to), and then do a comparison after that.
ourList =[ ] if ourList == [ ]: print("The list is empty") #The list is empty
We covered several ways to check for a list: by checking if emptiness is true or false, by using len(), and by comparing the list to an empty list. The first strategy we covered,
if list returns as false, is the one that’s recommended by the Python style guide. You can refer to Python documentation to see more about programming style guides as well as to see what is new in each new Python update.
Interested in learning more about Python? Check out this article on Python basics and how to get started on your path towards learning Python. Good luck!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.