DEV Community

Avnish
Avnish

Posted on

How to Check if a List is Empty in Python

Here are five examples demonstrating how to check if a list is empty in Python using different methods:

1. Using the not operator:

# Example 1
my_list = []

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

Explanation:

  • We initialize an empty list my_list.
  • We use the not operator in the if condition to check if the list is empty. If my_list is empty, not my_list evaluates to True, and the code inside the if block is executed.
  • If the list is not empty, the code inside the else block is executed.

Output:

The list is empty
Enter fullscreen mode Exit fullscreen mode

2. Using the len() function:

# Example 2
my_list = []

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

Explanation:

  • We initialize an empty list my_list.
  • We use the len() function to get the length of the list. If the length is equal to 0, then the list is empty.
  • If the length is 0, the code inside the if block is executed. Otherwise, the code inside the else block is executed.

Output:

The list is empty
Enter fullscreen mode Exit fullscreen mode

3. By comparing to an empty list:

# Example 3
my_list = []

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

Explanation:

  • We initialize an empty list my_list.
  • We compare my_list to an empty list []. If they are equal, then my_list is empty.
  • If my_list is empty, the code inside the if block is executed. Otherwise, the code inside the else block is executed.

Output:

The list is empty
Enter fullscreen mode Exit fullscreen mode

4. Using a list comprehension:

# Example 4
my_list = []

if not [elem for elem in my_list]:
    print("The list is empty")
else:
    print("The list is not empty")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We initialize an empty list my_list.
  • We use a list comprehension [elem for elem in my_list] to iterate over each element in my_list. If my_list is empty, the list comprehension will also be empty.
  • We apply the not operator to the list comprehension. If the list comprehension is empty, not will evaluate to True, indicating that my_list is empty.
  • If my_list is empty, the code inside the if block is executed. Otherwise, the code inside the else block is executed.

Output:

The list is empty
Enter fullscreen mode Exit fullscreen mode

5. Using the all() function:

# Example 5
my_list = []

if all(not elem for elem in my_list):
    print("The list is empty")
else:
    print("The list is not empty")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We initialize an empty list my_list.
  • We use a generator expression (not elem for elem in my_list) to create an iterable of boolean values indicating whether each element in my_list is empty or not.
  • We use the all() function to check if all elements in the iterable are True. If all elements are True, then my_list is empty.
  • If my_list is empty, the code inside the if block is executed. Otherwise, the code inside the else block is executed.

Output:

The list is empty
Enter fullscreen mode Exit fullscreen mode

These examples demonstrate different ways to check if a list is empty in Python, using various techniques such as boolean expressions, the len() function, list comparisons, list comprehensions, and the all() function.

Top comments (0)