How to Check If a List is Empty in Python

Here are different ways to check if a list is empty in Python:

  1. Using not operator(Boolean context)
  2. Using len() function
  3. Comparing with an Empty List
  4. Using the PEP 8 recommended method
  5. Using != operator
  6. Using NumPy Module

Method 1: Using not operator(Boolean context)

The Pythonic and concise way is to use the not operator. You can simply check the list directly in an if statement, and it returns true if the list is empty, otherwise false.

Visual Representation

Using not operator(Boolean context)

Example

players_list = []

if not players_list:
 print("Player list is empty")
else:
 print("Player list is not empty")

players_list_2 = ['Ronaldo']

if not players_list_2:
 print("Player list is empty")
else:
 print("Player list is not empty")

Output

Player list is empty
Player list is not empty

Method 2: Using len() Function

The len() function is used to find the number of elements in a list. If it returns zero, that means the list is empty; otherwise, it is not.

Visual Representation

Using len() Function

Example

players_list = []

if len(players_list) == 0:
 print("Player list is empty")
else:
 print("Player list is not empty")

players_list_2 = ['Ronaldo']

if len(players_list_2) == 0:
 print("Player list is empty")
else:
 print("Player list is not empty")

Output

Player list is empty
Player list is not empty

Method 3: Comparing with an Empty List

You can compare the list directly with an empty list [].

Visual Representation

Comparing with an Empty List

Example

players_list = []

if players_list == []:
 print("Player list is empty")
else:
 print("Player list is not empty")

players_list_2 = ['Ronaldo']

if players_list_2 == []:
 print("Player list is empty")
else:
 print("Player list is not empty")

Output

Player list is empty
Player list is not empty

Method 4: Using the PEP 8 recommended method

According to PEP 8(the style guide for Python), the Pythonic method to check for an empty list is to take advantage of the fact that empty lists are considered falsy.

Visual Representation

Using the PEP 8 recommended method

Example

players_list = []

if players_list:
 print("Player list is not empty")
else:
 print("Player list is empty")

Output

Player list is empty

Method 5: Using != operator

The != (not equal) operator checks if a list is not equal to an empty list and returns True if it is not empty, and False otherwise.

Example

players_list = []

if players_list != []:
 print("Player list is not empty")
else:
 print("Player list is empty")

Output

Player list is empty

Method 6: Using NumPy Module

First, you need to convert the list to a NumPy array and then check if the size attribute is zero.

Example

import numpy as np

players_list = []

np_array = np.array(players_list)

if np_array.size == 0:
 print("Player list is empty")
else:
 print("Player list is not empty")

Output

Player list is empty

Alternatively, you can use the numpy.size() function directly on the list.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.