How To Check If List Is Empty In Python?

Lists are the most commonly used data structures in python. It is used to store multiple items in a single object.

You can check if the list is empty using the len() function in python.

In this tutorial, you’ll learn how to check if the list is empty or not in python.

If You’re in Hurry…

You can use the below code snippet to check if the list is empty in Python.

This is the recommended method in PEP-8 and it is the best way to check if the list is empty.

Snippet

a =[]

if not a:
    print("List is empty")
else:
    print("List is not empty")

Output

    List is Empty

Snippet 2

a =[]

if a:
    print("List is not empty")
else:
    print("List is Empty")

Output

    List is Empty

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods available to check if the list is empty or not in python.

To create an empty list with specific size, read How To Create An Empty List in Python with a Certain Size

Using PEP-8 Recommended Method

You can check if the list is empty or not by using the list name in the If statement.

When you use the list in the IF statement, the length of the list is returned.

  • If the length is 0, it’s implicitly converted to False.
  • If the length is greater than 0, then it’s converted to True.

This method is also called Truth Value Testing.

Code

In the below example, you’re using the If not to check if the list is empty.
So you can implement the logic that needs to be executed when the list is empty in the If part.

a =[]

if not a:
    print("List is empty")
else:
    print("List is not Empty")

Output

    List is empty

Snippet

In the below example, you’re using only the If to check if the list is empty.
So you can implement the logic that needs to be executed when the list is not empty in the If part.

a =[]

if a:
    print("List is not empty")
else:
    print("List is Empty")

Output

    List is Empty

This is the fastest way to check if the list is empty in python.

Using The bool() Function

You can check if the list is empty in python using the bool() function.

bool() function returns boolean value of the specified object.

The object will always return True, unless the object is empty, like [], (), {}.

You can use the bool() function for any of the list-like objects.

Snippet

Use the below code snippet to check if the list is empty or not using the bool() function.

a =[]

if bool(a):
    print("list is not empty")
else:
    print("list is empty")

Output

    list is empty

Snippet

a =[]

if not bool(a):
    print("list is empty")
else:
    print("list is not empty")

Output

    list is empty

This is how you can use the bool() function to check if the list is empty or not in Python.

Using len() Function

In this section, you’ll learn how to use the len() function to check if the list is empty or not in python.

len() function returns the number of items in the list.

  • When the list is empty, the len() function returns 0, and the 0 is implicitly converted to False when used in the If statement.
  • When the list is NOT empty, It returns the length value. Values other than 0 are converted to True implicitly.

Snippet

Use the below snippet to check if the list is empty or not in python using the len() function and If not.

a = []

#Length returns 0 if list is empty. 0 is implicitly converted to #false when used in the IF statement

if not len(a):
    print('The list is empty')
else:
    print('list is not empty')

Output

    The list is empty

You can use the len() function alone to check if the list is not empty before performing any operation.

Snippet

a = []

#Length returns 0 if the list is empty. 0 is implicitly converted to false #when used in IF statement

if len(a):
    print('The list is not empty')
else:
    print('list is empty')

Output

    The list is empty

This is how you check if the list is empty or not in python using the len() function.

Using len() With Comparison Operator

You can use the len() function with the comparison operator and compare the result with 0 to check if the list is empty.

If the list is empty, then the If statement will be executed.

Snippet

a = []

if len(a) == 0:
    print('List is empty')
else:
    print('List not empty')

Output

    List is empty

This is how you can use the len() function with the comparison operator to check if the list is empty or not in python.

Comparison With Empty List

You can also compare the list object with the empty list to check if the list is empty.

An empty list is denoted using the []. When a list object is compared with [] using == operator, then it returns True if the list object is empty. Else it returns False.

Use the below snippet to check if the list is empty by comparing it with the empty list.

Snippet

a = []

if a == []:
    print('List is empty')
else:
    print('List is not empty')

Output

    List is empty

This is how you can use compare the list with the empty list to check if it’s empty or not.

Why You need to check If List is Empty

If you’re just checking if the list is empty or not Just to ensure it’s not empty before performing any operation, then you can pretty well use the list in the for loop or any other iterators directly. It’ll be executed only if the list has any items. Otherwise, it will not be executed.

Snippet

a = []

for element in a:
    print(element)

Conclusion

To summarize, you’ve learned how to check if a list is empty or not using the pep8 recommended method. It is the fastest way and the best way to check if the list is empty. You’ve also learned other methods available to check if the list is empty such as bool() function, len() function, compared with the empty list, and so on.

If you have any questions, comment below.

You May Also Like

Leave a Comment