Python - Check if list is empty or not
To check if a list is empty or not, we have to use the len(), bool() and not operator method in python.
In this article, we will learn different ways to check if a list is empty or not in python.
Python list is an order sequence that can hold multiple object types like integers, strings, and other objects in a single variable.
A list is an iterable object, so in some cases, we might want to check if the list has any items in it or if it's an empty list.
How to find out if a python list is empty
It is easy to find out an empty list in python because any empty sequence like ('',(),[]) will evaluate to False .
So, if we check an empty list with in-built python methods like len(), bool() it will return False.
Ways to check for empty list in python
To check if a Python list is empty, we can use any of the following methods:
- len() method
- not operator
- bool() method
- Comparing to an empty list
let's see methods with an example.
Using len() to check for empty lists in Python
The len() function returns the number of elements in a list in python. If len() returns zero it means the list is empty.
Example:
my_list = [] if len(my_list) == 0: print('List is Empty') else: print('List is not Empty')
Output:
List is Empty
Here, we have an empty list in a variable my_list.
We used the equality operator (==) to compare the result of len() function with 0. Since the len(my_list) returned zero, the comparison returned True too. And thus the if statement executed the first condition and printed out "List is Empty".
Using not operator to check for empty Python List
The not operator is a logical operator in python. It changes a boolean value to the opposite with the not keyword.
If the value is True, it will change it to False (not True) and if the value is False, it will change it to True (not False)
As we know, any empty sequence will evaluate to False, the not operator will switch it to True. For example.
my_list = [] if not my_list: print('List is Empty') else: print('List is not Empty')
Output:
List is Empty
Here, we have a variable (my_list) with an empty list. Next, we use the if statement and the not operator to check if the list is empty or not.
Now, if the list is empty the not my_list statement will turn True, and so it will yield out "List is Empty" in the terminal.
Using bool() method to check for empty list in python
The bool() function is used to return a boolean value i.e True or False, of any specified object in python.
list1 = [] list2 = [1,2,3] print(bool(list1)) # False print(bool(list2)) # True
So, we can use the bool() method to find an empty Python list like this.
Example:
my_list = [] if bool(my_list): print('List is Not Empty') else: print('List is empty')
Output:
List is Empty
Here, we have passed our python list in the bool function and since the list is empty it returns False. And so it executes the else statement i.e List is Empty.
Check if list is empty by comparing it with an empty list []
We can use the equality operator (==) to compare our python list with an empty list ([]). It will return True, if both the list are equal, if not it will return False.
We can use the if statement to print out a desired output on the terminal.
Example:
my_list = [] if my_list == []: print('List is Empty') else: print('List is not empty')
Output:
List is Empty
Since the list is empty, it runs the first block of the if statement and prints "List is Empty" in the terminal.
Conclusion: In this post, we have learned how to check if a python list is empty using in-built python functions like bool() and len(), using not operator and also using equality operator.
Related topics:
Related Posts
How to merge JSON files in python
Learn different techniques to merge multiple JSON files in Python using built-in JSON module and Pandas library. Includes examples for combining data arrays and merging dictionaries by key.
Pytube Description and Keyword Not Showing: Solution
Solve the issue with Pytube not showing youtube description and tags in this article.
How to Fix the subprocess-exited-with-error in Python
Learn what cause the subprocess-exited-with-error in Python and also learn how to solve the error in your code.
Python Integer Division: The Floor Division Operator Explained
Article on Python Integer Division operator (//) with examples and the difference between standard division and floor division.
