Sling Academy
Home/Python/Python: How to Check If a List Is Empty (3 Approaches)

Python: How to Check If a List Is Empty (3 Approaches)

Last updated: June 06, 2023

In Python, a list is a data structure that can store multiple values of different types in a sequential order. An empty list is a list that contains no elements or items. It is a falsy value.

This short article will show you the most three common ways to check whether a list is empty or not in Python.

Using the len() function

The idea here is simple: use the len() function to check the length of the list. If the length is 0, then the list is empty.

Example:

# Create an empty list
my_list = []

# Check if the list is empty using the len() function
if len(my_list) == 0:
    print("The list is empty")
else:
    print("The list is not empty")

Output:

The list is empty

Using the not operator

You can use the not operator to check the emptiness of a list. This operator returns True if its operand is a falsy value, and False if its operand is a truthy value. As mentioned earlier, an empty list is a falsy value in Python, which means that it evaluates to False in a boolean context. Thus, using the not operator on an empty list will return True.

Example:

# create an empty list
my_list = []

# check if the list is empty using the not operator
if not my_list:
    print("The list is empty")
else:
    print("The list is not empty")

Output:

The list is empty

Using the == operator

The third solution is to compare your list with an empty list ([]] by using the == operator as shown in the example below:

my_list = [] 

if my_list == []:
    print("List is empty")
else:
    print("List is not empty")

Output:

List is empty

Next Article: Python: Replacing/Updating Elements in a List (with Examples)

Previous Article: Python: Counting the Number of Elements in a List (4 Examples)

Series: Python List Tutorials (Basic and Advanced)

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots