How to Check if a List is Empty in Python

Python offers different data structures to store and manipulate the data effectively. One such data structure is List which can store the data of similar as well as distinct data types. While working with the List data structure, you may need to find if the targeted list contains some elements or if it’s empty. To serve this particular purpose, different methods can be used in Python.

How to Check/Find if the List is Empty in Python?

A list is a collection/group of elements enclosed in square “[ ]” brackets and every single element of the list is separated through a comma. A list containing the items of the same data types is classified as a homogenous list and the list which contains elements of different datatypes is a heterogeneous list. 

Several methods to see if the list contains any data or is vacant are:

  1. Using PEP 8 
  2. Using len() 
  3. Using __len__()
  4. Using == Operator
  5. Using implicit booleans
  6. Using != operator
  7. Using not operator 
  8. Comparing the Given List With the Empty List 
  9. Using try/except 
  10. Using NumPy 

Let us discuss each method to determine whether the list is empty.

Method 1: Using the PEP 8 

The PEP 8 method can be referred to as the Python Enhancement Proposal that gives various styling guidelines to write clean code. The purpose of using this method is to improve the code’s structure. The PEP method provides information about how to write the class and variable names, how many spaces to give, the naming convention, and much more.

PEP 8 is a recommended styling method that can be considered the most Pythonic way to examine if the list is empty or not in Python. The following code uses the PEP method to see if the list is empty/blank:

list1 = []
list2=[1,2,3]
def examine_empty(list):
if (list):
  print('The selected list is not empty')
else:
  print('The selected list is empty')
examine_empty(list1)
examine_empty(list2)

In the above code,

  • A user-defined function named “examine_empty” accepts a list as an argument
  • This method utilizes “truth value testing”. The status of the list is examined using its bool value.

Output

The following output displays if a list is empty/blank using the PEP 8 method:

Method 2: Using len()

The most convenient way to find if an iterable (like a List) is empty in Python is to use the len() function. The len() function gives information about the number of items inside a Python object. The following code explains how to find if a list contains any element using the Python len() function:

list1 = []
list2=[1,2,3]
def examine_empty(list):
if len(list) == 0:
  print('The list is empty')
else:  print('The selected list has some elements')
examine_empty(list1)
examine_empty(list2)

Here, in the above code,

  • The examine_empty is a user-defined function that gets a list as an argument.
  • len() gets the list’s length. If it is zero the list has no element. In the other scenario, the list contains data.
  • The function examine_empty is called on different lists.

Output

The following output shows if the list has any element inside it using the len() function in Python:

Method 3: Using __len__()

Dispatching the “__len__()” function on the list object can get the length of the object. The __len__() function can be used to see if the list contains any element in Python and the following code shows how it can be done:

list1 = []
list2=[1,2,3]
def examine_empty(list):
if (list.__len__()==0):
  print('The list is  empty')
else:
  print('The selected list has some elements')
examine_empty(list1)
examine_empty(list2)

In the above code,

  • The __len__() function is called on the list object. If the list’s size is equal to 0, this means the list has no elements. The status of the list will be then printed.
  • The function is called on different list inputs.

Output

The following output displays how we can find if a List is empty or not using the __len__() function in Python:

Method 4: Using Equality Operator (==)

The equality operator can be used in an if-else block to see if the list is equal to an empty list. The following code shows how to examine if a list is empty using the equality operator:

list1 = []
list2=[1,2,3]
def examine_empty(list):
if (list==[]):
  print('The list is  empty')
else:
  print('The selected list has some elements')
examine_empty(list1)
examine_empty(list2)

In the above code,

  • The examine_empty function examines if the list is equal to an empty list using the “==” operator.
  • If it is equal to the empty list, the list will not contain any element. The results will be printed likewise.
  • The function is called on different list inputs.

Output

The output displays if the list contains data or if it’s empty using the == operator:

Method 5: Using a Boolean

The bool() method retrieves True or False according to the given list’s items, as demonstrated in the following code snippet:

list1 = []
list2=[1, 2, 3]
def examine_empty(list):
if bool(list):
  print('The selected list has some elements')
else:
  print('Given list is empty')
examine_empty(list1)
examine_empty(list2)

In the above code,

  • The examine_empty function takes a parameter “list”.
  • The parameter is converted to a bool value.
  • In the case of the empty list, the retrieved value will be True, while if the list has some elements then the resultant value will be false.
  • The examine_empty function checks if the list is empty or not depending upon the results from the bool expression.
  • The examine_empty function is called on different lists to test its functionality.

Output

Here is the output of the stated bool() function:

Method 6: Using != Operator 

Another useful way of checking whether the list is empty or has some elements in it is by utilizing the “!=” operator. This is similar to the == operator but when the condition is true, it will print that the list is not empty. Here is a practical demonstration of the != operator:

list1 = ['A', 'B', 'C']
list2 = []
def examine_empty(list):
    if len(list) != 0:
        print('Selected list has some elements')
    else:
        print('Given list is empty')
examine_empty(list1)
examine_empty(list2)

In the above code,

  • The examine_empty function takes the argument list.
  • If the length/size of the selected list is not equal to 0, the function prints “The list has some elements” else it will print “The list is empty”.
  • The examine_empty function is called on different input lists.

Output

The output displays how to examine whether the list has elements using the != operator:

Method 7: Using Not Operator

The “Not” operator can be used to examine if the list contains any data. The following code illustrates the use of the “Not” operator to verify the existence of elements in a list:

list1 = ['A', 'B', 'C']
list2 = []
def examine_empty(list):
    if not list:
        print('Given list is empty')
    else:
        print('The selected list has some elements')
examine_empty(list1)
examine_empty(list2)

In the above code,

  • The examine_empty function gets a list as an argument.
  • The not keyword implies that if the list does not contain any data, the condition is True and it will print the status of the list accordingly.
  • The examine_empty function is called on different list inputs.

Output

The output shows how to examine if the list is empty using the not operator:

Method 8: By Comparing With an Empty List 

The given list can be compared with a blank list to check if the selected list has some elements or if it’s empty. The following code demonstrates this concept better:

def examine_empty(list):
    if  list==[]:
        print('The list is empty')
    else:
        print('The list is not empty')list1 = ['A', 'B', 'C']
list2 = []
list3= [190]
examine_empty(list1)
examine_empty(list2)
examine_empty(list3)

In the above code,

  • A user-defined function named examine_empty() accepts a list as an argument.
  • With a comparison of the list with [], the status of the input list is printed. If it equates to the empty list it means that the list contains no data.
  • The examine_empty function is called on different list inputs.

Output

The following code displays how to examine if a list is empty or not by comparing the list with an empty list:

Method 9: Using the try/except Block

Try/except block inspects if the list contains any data or not. The algorithm accesses the first element in the list to examine the empty list. The following code explains how to examine if the list is empty or not by accessing the first element of the list inside a try/except block:

list1 = []
list2 = []
list3= ["I LOVE TO CODE!"]
def examine_empty(list):
    try:
        list[0]
        print('The list is not empty')
    except IndexError:
        print('The list is  empty')
examine_empty(list1)
examine_empty(list2)
examine_empty(list3)

In the above code,

  • The examine_empty function takes the list as an argument.
  • Inside the try-except block, the list[0] gets the first element/item of the list. If the first element exists, it will print “The list is not empty” otherwise it will give an exception of IndexError and it can be handled by printing “The list is empty”.
  • The examine_empty function is then called on different input lists.

Output

The following output displays how to examine if the list is empty or not by accessing the first element of the list inside a try/except block:

Method 10: Using the NumPy Module

Methods from the NumPy module can tell if the given list includes any element or not. The size function from the NumPy module can be used to see if the list is empty or not. It gives the size of the list. Having the list size as 0 means that the list contains no data. In any other case, the string will not be empty. The following code explains how to examine if the list is empty or not using the size method from the NumPy module:

import numpy as np
sample_list1 = []
sample_list2= ["I LOVE TO CODE!"]
def examine_empty(list):
    res_array=np.array(list)
    if  res_array.size==0:
        print('The list is empty')
    else:
        print('The list is not empty')
examine_empty(sample_list1)
examine_empty(sample_list2)

In the above code,

  • The examine_empty function takes a list as an argument which is converted to an array using np.array() and its result is stored in res_array.
  • The examine_empty function examines the size of the array using the size method from the NumPy module.
  • The size of the length equating to 0 suggests that the list is empty otherwise it will contain some data.
  • The examine_empty() function is called on different lists.

Output

The following output displays how to examine if the list is empty or not using the size method from the NumPy module:

This sums up how to check whether the given list is empty or not.

Conclusion

Various Methods like len(), bool, and size() functions from the NumPy module can be used to know if the list contains any data or not. Having information about whether the list is empty or not is useful in handling data like doing data analysis or validation. This article has explained different methods that are used to examine if a list is empty or not and illustrates each method with the help of an example.