• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Check if List is Empty in Python

How to Check if List is Empty in Python

February 9, 2022 Leave a Comment

We can easily check if a list is empty in Python. An empty list has length 0, and is equal to False, so to check if a list is empty, we can just check one of these conditions.

empty_list = []

#length check
if len(empty_list) == 0:
    print("List is empty!")

#if statement check
if empty_list:
    print("List is empty!")

#comparing to empty list
if empty_list == []:
    print("List is empty!")

In Python, lists are a collection of objects which are unordered. When working with lists, it can be useful to be able to easily determine if the list is empty.

There are a few ways you can determine if a list is empty.

Of course, you can always test to see if the list is equal to another empty list. Second, the length of an empty list is 0. Finally, when converting an empty list to a boolean value, we get False.

In this case, we can use any one of these conditions to determine if a list is empty or not.

In the following Python code, you can see the three ways you ca check if a list is empty or not.

empty_list = []

#length check
if len(empty_list) == 0:
    print("List is empty!")

#if statement check
if empty_list:
    print("List is empty!")

#comparing to empty list
if empty_list == []:
    print("List is empty!")

Checking if List is Empty with if Statement in Python

One fact we can use in Python to check if a list is empty is that a list that is empty is equivalent to the boolean value False.

In this case, we can test if a list is empty using a simple if statement.

empty_list = []

#if statement check
if empty_list:
    print("List is empty!")

Checking if List is Empty Using Python len() Function

One of the ways we can easily check if a list is empty in Python is with the Python len() function.

The length of a list which is empty is 0.

Checking to see if a list is empty using the Python len() function is shown in the following Python code.

empty_list = []

if len(empty_list) == 0:
    print("List is empty!")

Checking if List is Empty By Comparing to Another Empty List in Python

You can also check if a list is empty by comparing it to another empty list. This is the most obvious method and works if you want to check if a tuple is empty, or check if a set is empty.

Below is how to compare an empty list to another list to determine if the other list is empty or not.

empty_list = []

if empty_list == []:
    print("List is empty!")

Hopefully this article has been useful for you to learn how to check if a list is empty in Python.

Other Articles You'll Also Like:

  • 1.  Get Name of Function in Python
  • 2.  Drop First n Rows of pandas DataFrame
  • 3.  Convert timedelta to Seconds in Python
  • 4.  Get Current URL with Selenium in Python
  • 5.  Random Number Without Repeating in Python
  • 6.  Writing a Table Faster to Word Document Using python-docx
  • 7.  Change Python Turtle Shape Fill Color with fillcolor() Function
  • 8.  Truncate Decimal from Number with Python math.trunc() Function
  • 9.  How to Group By Columns and Find Variance in pandas
  • 10.  Replace Values in Dictionary in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy