How to check if a list is empty in Python?

Considering that Python list is one of the most used data structures, it’s a given that at some point the need to check if a list is empty will arise.

There are several ways to do this, however, the most straightforward is to use a method called Trut…

Considering that Python list is one of the most used data structures, it’s a given that at some point the need to check if a list is empty will arise.

There are several ways to do this, however, the most straightforward is to use a method called Truth Value Testing.

In Python, any object can be tested for truth value, including lists. In this context, lists are considered truthy if they are non-empty, meaning that at least one argument was passed to the list. So, if you want to check if a list is empty, you can simply use the not operator.

empty_list = []
non_empty_list = [1, 2, 3]

not empty_list # True
not non_empty_list # False

The not operator is a logical operator that returns the opposite of the value it is applied to. In the above code example we can see that it returns True if the list is empty and False if the list is not empty.

While the not operator is the most straightforward and pythonic way to check if a list is empty, there are other ways to do this. For example, you can use the len() function. This function returns the length of the list, which is 0 if the list is empty.

empty_list = []
non_empty_list = [1, 2, 3]

len(empty_list) == 0 # True
len(non_empty_list) == 0 # False

Another way is to compare the list to an empty list using the == operator.

empty_list = []
non_empty_list = [1, 2, 3]

empty_list == [] # True
non_empty_list == [] # False

Print Share Comment Cite Upload Translate
APA
Isabelle M. | Sciencx (2024-03-28T12:25:40+00:00) » How to check if a list is empty in Python?. Retrieved from https://www.scien.cx/2022/12/11/how-to-check-if-a-list-is-empty-in-python/.
MLA
" » How to check if a list is empty in Python?." Isabelle M. | Sciencx - Sunday December 11, 2022, https://www.scien.cx/2022/12/11/how-to-check-if-a-list-is-empty-in-python/
HARVARD
Isabelle M. | Sciencx Sunday December 11, 2022 » How to check if a list is empty in Python?., viewed 2024-03-28T12:25:40+00:00,<https://www.scien.cx/2022/12/11/how-to-check-if-a-list-is-empty-in-python/>
VANCOUVER
Isabelle M. | Sciencx - » How to check if a list is empty in Python?. [Internet]. [Accessed 2024-03-28T12:25:40+00:00]. Available from: https://www.scien.cx/2022/12/11/how-to-check-if-a-list-is-empty-in-python/
CHICAGO
" » How to check if a list is empty in Python?." Isabelle M. | Sciencx - Accessed 2024-03-28T12:25:40+00:00. https://www.scien.cx/2022/12/11/how-to-check-if-a-list-is-empty-in-python/
IEEE
" » How to check if a list is empty in Python?." Isabelle M. | Sciencx [Online]. Available: https://www.scien.cx/2022/12/11/how-to-check-if-a-list-is-empty-in-python/. [Accessed: 2024-03-28T12:25:40+00:00]
rf:citation
» How to check if a list is empty in Python? | Isabelle M. | Sciencx | https://www.scien.cx/2022/12/11/how-to-check-if-a-list-is-empty-in-python/ | 2024-03-28T12:25:40+00:00
https://github.com/addpipe/simple-recorderjs-demo