DEV Community

Avnish
Avnish

Posted on • Edited on

How to Check if a List is Empty in Python

Python isEmpty() Equivalent – How to Check if a List is Empty

In Python, lists are powerful data structures that allow you to store and manipulate collections of items. However, there are scenarios where you need to ensure a list is not empty before performing operations like iteration or data manipulation.

Python doesn’t have a built-in isEmpty() method, but there are several straightforward ways to check if a list is empty. In this guide, we’ll explore these methods step by step with clear explanations and examples.


1. Using the not Operator

The not operator in Python is a simple and effective way to check if a list is empty. When used with a list, not evaluates to True if the list has no elements, and False otherwise.

Example:

people_list = []  # An empty list

if not people_list:
    print("The list is empty.")
else:
    print("The list is not empty.")
Enter fullscreen mode Exit fullscreen mode

How It Works:

  1. The not operator negates the truth value of its operand.
  2. An empty list in Python is considered False in a boolean context.
  3. If not people_list evaluates to True, the list is empty.

Output:

The list is empty.
Enter fullscreen mode Exit fullscreen mode

Adding Elements to the List:

Try modifying the list to see the behavior:

people_list = ["Alice", "Bob"]

if not people_list:
    print("The list is empty.")
else:
    print("The list is not empty.")
Enter fullscreen mode Exit fullscreen mode

Output:

The list is not empty.
Enter fullscreen mode Exit fullscreen mode

2. Using the len() Function

The len() function returns the number of elements in a list. If the length is 0, the list is empty.

Example:

people_list = []  # An empty list

if len(people_list) == 0:
    print("The list is empty.")
else:
    print("The list is not empty.")
Enter fullscreen mode Exit fullscreen mode

How It Works:

  1. len(people_list) calculates the number of elements in people_list.
  2. If the result is 0, the list is empty.

Output:

The list is empty.
Enter fullscreen mode Exit fullscreen mode

Adding Elements to the List:

Modify the list and observe the changes:

people_list = ["John", "Jane"]

if len(people_list) == 0:
    print("The list is empty.")
else:
    print("The list is not empty.")
Enter fullscreen mode Exit fullscreen mode

Output:

The list is not empty.
Enter fullscreen mode Exit fullscreen mode

3. Comparing to an Empty List

Another method to check if a list is empty is by directly comparing it to an empty list ([]).

Example:

people_list = []  # An empty list

if people_list == []:
    print("The list is empty.")
else:
    print("The list is not empty.")
Enter fullscreen mode Exit fullscreen mode

How It Works:

  1. The comparison people_list == [] checks if people_list has the same elements as an empty list.
  2. If true, it means the list is empty.

Output:

The list is empty.
Enter fullscreen mode Exit fullscreen mode

Adding Elements to the List:

Update the list and test the comparison:

people_list = ["Eve", "Adam"]

if people_list == []:
    print("The list is empty.")
else:
    print("The list is not empty.")
Enter fullscreen mode Exit fullscreen mode

Output:

The list is not empty.
Enter fullscreen mode Exit fullscreen mode

Which Method Should You Use?

  • not Operator:

    • Simplest and most Pythonic.
    • Best for readability and minimal code.
  • len() Function:

    • Explicit and great for beginners who find not less intuitive.
    • Can be used when the length of the list is needed for further operations.
  • Comparing to an Empty List:

    • Clear and explicit but slightly less efficient since it creates a new empty list for comparison.

Additional Example: Combining Checks

Sometimes, you may want to check a list's state and take different actions based on its content.

people_list = ["Alice", "Bob"]

if not people_list:
    print("The list is empty.")
elif len(people_list) == 1:
    print("The list has one element.")
else:
    print("The list has multiple elements.")
Enter fullscreen mode Exit fullscreen mode

Output:

The list has multiple elements.
Enter fullscreen mode Exit fullscreen mode

Summary

In this article, we explored three effective ways to check if a list is empty in Python:

  1. Using the not operator.
  2. Using the len() function.
  3. Comparing the list to an empty list ([]).

Each method has its strengths and is suited for different situations. For most use cases, the not operator is the preferred, Pythonic way to check if a list is empty. Experiment with these methods to find what works best for your specific needs!

Top comments (0)