Python – Check if a list is empty or not
In this article, we will learn How to check if the given list is empty or not.
Example:
Input: ['Geeks', 'Geeks', 'Geeks', 'Geeks', ] Output: Yes Not Empty Input: [ ] Output: Empty
How to check if a list is empty in Python
Method 1: Check the empty list using the len() With Comparison Operator
Let’s see how we can check whether a list is empty or not, in a less pythonic way. We should avoid this way of explicitly checking for a sequence or list
Python3
# Python code to check for empty list # Explicit way def Enquiry(lis1): if len (lis1) = = 0 : return 0 else : return 1 # Driver Code lis1 = [] if Enquiry(lis1): print ( "The list is not empty" ) else : print ( "Empty List" ) |
Output:
Empty List
Time complexity: O(n)
Auxiliary space: O(n), where n is length of list
Method 2: Check the empty list using the implicit booleans
Now let’s see a more pythonic way to check for an empty list. This method of a check is an implicit way of checking and more preferable than the previous one.
Python3
# Python code to check for empty list # IMPLICIT way or Pythonic way def Enquiry(lis1): if not lis1: return 1 else : return 0 # Driver Code lis1 = [] if Enquiry(lis1): print ( "The list is Empty" ) else : print ( "The list is not empty" ) |
Output:
The list is Empty
Method 4: Check the empty list using the PEP 8 recommended method
This is another method that allows us to determine whether a list in Python is empty. The most Pythonic method of checking the same is shown below.
Python3
list1 = { "a" : 1 , "b" : 2 , "c" : 3 } list2 = [] if list2: print ( "list is not empty" ) else : print ( "list is empty" ) |
Output:
list is empty
Numpythonic way
Example 1
The previous methods that we used in normal Python don’t work for the Numpythonic way. Other methods that work fine for lists or other standard containers fail for NumPy arrays. This way fails with NumPy arrays because Numpy tries to cast the array to an array of bools and if this tries to evaluate all of those bools at once for some kind of aggregate truth value, it fails so we get a ValueError.
Python3
# Numpythonic way with the previous method # Returns ValueError import numpy def Enquiry(lis1): return (numpy.array(lis1)) # Driver Code lis1 = [ 0 , 1 ] if Enquiry(lis1): print ( "Not Empty" ) else : print ( "Empty" ) |
Output:
None
Error:
Traceback (most recent call last): File "/home/2d237324bb5211d7216c521441a750e9.py", line 7, in if Enquiry(lis1): ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Example 2
In this example, we will see that even if the list is Not Empty, the output will show Empty. If the list contains one 0, then the if statement will incorrectly result.
Python3
# Numpythonic way with the previous method # Returns wrong result import numpy def Enquiry(lis1): return (numpy.array(lis1)) # Driver Code lis1 = [ 0 , ] if Enquiry(lis1): print ( "Not Empty" ) else : print ( "Empty" ) |
Output:
Empty
Making the Numpythonic way work
Example 1
If we have a NumPy array then the correct method in all cases is to use if .size. This size checks the size of the arrays and returns True or False accordingly. Example:
Python3
# Numpythonic way to check emptiness # Use of size import numpy def Enquiry(lis1): return (numpy.array(lis1)) # Driver Code lis1 = [] if Enquiry(lis1).size: print ( "Not Empty" ) else : print ( "Empty" ) |
Output:
Empty
Example 2
This example shows the other case with a single 0 element, which failed in the previous cases.
Python3
# Numpythonic way to check emptiness # Use of size import numpy def Enquiry(lis1): return (numpy.array(lis1)) # Driver Code lis1 = [ 0 , ] if Enquiry(lis1).size: print ( "Not Empty" ) else : print ( "Empty" ) |
Output:
Not Empty
Method 5 : Comparing given list with empty list using != operator
Python3
# Python code to check for empty list lis1 = [] if lis1! = []: print ( "The list is not empty" ) else : print ( "Empty List" ) |
Empty List
Method: Using list comprehension + len()
Python3
lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' , ] x = [ "not empty" if len (lst)> 0 else "empty" ] print (x) |
['not empty']
Using the any() function:
The any() function returns True if any element in an iterable is True, and False otherwise. This can be used to check if a list is empty or not by passing the list to any() as an argument.
Here is an example of how to use any() to check if a list is empty:
Python3
def is_empty(lst): return not any (lst) lst1 = [ 1 , 2 , 3 ] lst2 = [] print (is_empty(lst1)) # Output: False print (is_empty(lst2)) # Output: True |
False True
Method : Comparing given list with empty list using == operator
Python3
# Python code to check for empty list lis1 = [] if lis1 = = []: print ( "Empty List" ) else : print ( "The list is not empty" ) |
Empty List
This approach has the advantage of being concise and easy to understand. It is also generally faster than other approaches that involve looping through the elements of the list.
Please Login to comment...