
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a List is Empty in Python
In Python, a list is an ordered sequence that can hold several object types, such as integers, characters, or floats. In this article, we will show you how to check if the given input list is empty or NOT using Python. Below are the 5 methods to accomplish this task -
Assume we have taken an empty list. We will check if the input list is empty or not and return some random message for acknowledgment using different methods as specified above.
Method 1: Using the NOT operator
In the example below, we are going to use the not operator. So first we need to create a variable to store the input empty list. Then check if the list is empty or not by using an if condition and the not operator. If the list is empty, then it returns True; otherwise, False, which means the list is not empty.
Example
The following program checks whether the input list is empty or not using the not operator -
# empty list lst = [1,2,3,4,5] # evaluating empty list object to False if not lst: print('Empty list') else: print('List is not Empty \n',lst)
When you run the program, it will show this output -
('List is not Empty \n', [1, 2, 3, 4, 5])
Method 2: Using the len() function
Here we will use the len() function to check whether the list is empty or not. So, first create a variable to store the input empty list. Check whether the length of the list is equal to 0 using the len() function in an if conditional statement. Print an Empty list if the length of the list is equal to 0. Else, print "List is not empty".
Example
The following program checks whether the input list is empty or not using the len() function -
# empty list lst = [] # Checking whether the list size is equal to 0 if len(lst) == 0: print('Empty list') else: print('Not Empty list')
After running the program, you will get this result -
Empty list
Method 3: By Comparing with an Empty List
Now we will compare with the empty list. So we are using the if conditional statement to check that the list object is pointing to the literal [] i.e checking whether the list is equal to [] or not. Print "Empty list", if the condition is true.
Example
The following program checks whether the input list is empty or not using the empty list[] literal -
# empty list lst = [] # Checking whether the list object is equal to [](null list) if lst == []: print('Empty list') else: print('List is not empty\n',lst)
This output will be displayed when the program runs -
Empty list
Method 4: Using __len__()
The size of a list can be obtained by invoking the __len__() function on the list object. The list is empty if the list size equals zero.
Example
The following program checks whether the input list is empty or not using the __len__() function -
# empty list lst = [] # Checking whether the list size is equal to 0 if lst.__len__() == 0: print('Empty list') else: print('Not Empty list')
You will see this result after executing the program -
Empty list
Method 5: Using NumPy Module
Now we will use the import keyword to import the numpy module. Using the numpy.array() function to convert a list to a NumPy array and create a variable to store it. Then use the if conditional statement to check whether the numpy array size is equal to 0 with the size attribute. Print "Empty list", if the condition is true, otherwise "List is not Empty".
Example
The following program checks whether the input list is empty or not using the NumPy module and the size attribute -
# importing NumPy module import numpy as np # empty list lst = [] # converting the list to NumPy array resultarray = np.array(lst) # checking whether the array size is equal to 0 if resultarray.size == 0: print('Empty list') else: print('List is not Empty')
The program gives this output when it is run -
Empty list