How to check if a list is empty in Python?


In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float.

In this article, we will show you how to check if the given input list is an empty list or NOT using python. Below are the 5 methods to accomplish this task −

  • Using not operator

  • Using len() function

  • By Comparing with an Empty List

  • Using __len__()

  • Using NumPy Module

Assume we have taken an empty list. We will check if the input list is empty or not and returns some random message for acknowledgment using different methods as specified above.

Method 1: Using not operator

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input empty list.

  • Check if the list is empty or not by using if condition and not operator

  • If the list is empty then (not list) returns True

  • If the (not list) returns False then 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)

Output

On executing, the above program will generate the following output −

('List is not Empty \n', [1, 2, 3, 4, 5])

Method 2: Using len() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input empty list.

  • Check whether the length of the list is equal to 0 using the len() function (The number of items in an object is returned by the len() method. The len() function returns the number of characters in a string when the object is a string) in an if conditional statement.

  • Print 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')

Output

On executing, the above program will generate the following output −

Empty list

Method 3: By Comparing with an Empty List

[] This denotes an empty list. So, by comparing our list object to [], we can determine whether the list is empty or not.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input empty list.

  • Use the if conditional statement to check whether 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.

  • Else print “List is not empty”, if the condition is false.

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)

Output

On executing, the above program will generate the following output −

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.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input empty list.

  • Use the if conditional statement to check whether the length of the list is equal to 0 using the __len__() function(The __len__ function in Python returns a positive integer representing the length of the object on which it is called. It implements the inbuilt len() function)

  • Print “Empty list”, If the list length 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 lst.__len__() == 0: print('Empty list') else: print('Not Empty list')

Output

On executing, the above program will generate the following output −

Empty list

Method 5: Using NumPy Module

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the numpy module

  • Create a variable to store the input empty list.

  • Use the numpy.array() function to convert a list to NumPy array and create a variable to store it.

  • 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.

  • Else print “List is not Empty”, if the condition is false.

Example

The following program checks whether the input list is empty or not using the NumPy module and 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')

Output

On executing, the above program will generate the following output −

Empty list

Conclusion

This article taught us how to use the not operator to determine whether a statement is true or false. To determine the length of a list, we learned how to use the len() function. The length of a tuple, dictionary, string, etc. can be determined using this. Additionally, we learnt how to create a NumPy array from a list and how to determine the size and length of a NumPy array.

Updated on: 23-Aug-2023

56K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements