How to Check If a List Is Not Empty in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a list is not empty in Python. This lab explores different methods to identify non-empty lists, focusing on boolean evaluation.

You'll start by creating a Python script named list_check.py to examine how Python interprets non-empty and empty lists in a boolean context. By running the script with both non-empty and empty lists, you'll observe the different outputs and understand how to effectively determine if a list contains any elements.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) python/BasicConceptsGroup -.-> python/booleans("Booleans") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") python/DataStructuresGroup -.-> python/lists("Lists") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") python/PythonStandardLibraryGroup -.-> python/data_collections("Data Collections") subgraph Lab Skills python/booleans -.-> lab-559534{{"How to Check If a List Is Not Empty in Python"}} python/conditional_statements -.-> lab-559534{{"How to Check If a List Is Not Empty in Python"}} python/lists -.-> lab-559534{{"How to Check If a List Is Not Empty in Python"}} python/build_in_functions -.-> lab-559534{{"How to Check If a List Is Not Empty in Python"}} python/data_collections -.-> lab-559534{{"How to Check If a List Is Not Empty in Python"}} end

Explore Non-Empty Lists

In this step, you will learn about non-empty lists in Python and how to identify them. A list is a fundamental data structure in Python that can hold an ordered collection of items. An empty list is a list that contains no items, while a non-empty list contains at least one item. Understanding how to work with lists is crucial for many programming tasks.

First, let's create a Python script to explore non-empty lists. Open the VS Code editor in the LabEx environment and create a new file named list_check.py in the ~/project directory.

## ~/project/list_check.py
my_list = [1, 2, 3]

if my_list:
    print("The list is not empty.")
else:
    print("The list is empty.")

In this script, we create a list called my_list containing the numbers 1, 2, and 3. The if my_list: statement checks if the list is non-empty. In Python, an empty list evaluates to False in a boolean context, while a non-empty list evaluates to True.

Now, let's run the script to see the output. Open the terminal in VS Code and navigate to the ~/project directory (you should already be in this directory by default). Then, execute the script using the python command:

python list_check.py

You should see the following output:

The list is not empty.

This confirms that our script correctly identifies a non-empty list.

Now, let's modify the script to use an empty list and see what happens. Change the my_list variable to an empty list:

## ~/project/list_check.py
my_list = []

if my_list:
    print("The list is not empty.")
else:
    print("The list is empty.")

Run the script again:

python list_check.py

This time, you should see the following output:

The list is empty.

This demonstrates that an empty list is correctly identified as empty.

This simple example shows how you can use the boolean evaluation of lists to determine whether they are empty or non-empty. This is a common technique in Python programming.

Check len() > 0

In this step, you will learn how to use the len() function to check if a list is non-empty by verifying if its length is greater than 0. The len() function returns the number of items in a list. By comparing the length of a list to 0, you can determine whether the list is empty or non-empty.

Let's modify the list_check.py script from the previous step to use the len() function. Open the list_check.py file in the VS Code editor in the ~/project directory.

## ~/project/list_check.py
my_list = [1, 2, 3]

if len(my_list) > 0:
    print("The list is not empty.")
else:
    print("The list is empty.")

In this script, we use the len(my_list) function to get the length of the list. The if len(my_list) > 0: statement checks if the length of the list is greater than 0. If it is, the list is non-empty; otherwise, it is empty.

Now, let's run the script to see the output. Open the terminal in VS Code and navigate to the ~/project directory (you should already be in this directory by default). Then, execute the script using the python command:

python list_check.py

You should see the following output:

The list is not empty.

This confirms that our script correctly identifies a non-empty list using the len() function.

Now, let's modify the script to use an empty list and see what happens. Change the my_list variable to an empty list:

## ~/project/list_check.py
my_list = []

if len(my_list) > 0:
    print("The list is not empty.")
else:
    print("The list is empty.")

Run the script again:

python list_check.py

This time, you should see the following output:

The list is empty.

This demonstrates that an empty list is correctly identified as empty using the len() function.

Using the len() function to check if a list is non-empty is a common and explicit way to determine whether a list contains any items. It is often preferred for its readability and clarity.

Use Boolean Evaluation

In this step, you will learn how to directly use the boolean evaluation of a list to determine if it's non-empty. In Python, lists (and other data structures) can be directly evaluated in a boolean context. An empty list evaluates to False, while a non-empty list evaluates to True. This allows for concise and readable code.

Let's modify the list_check.py script from the previous steps to use boolean evaluation. Open the list_check.py file in the VS Code editor in the ~/project directory.

## ~/project/list_check.py
my_list = [1, 2, 3]

if my_list:
    print("The list is not empty.")
else:
    print("The list is empty.")

In this script, the if my_list: statement directly checks if the list is non-empty. Python automatically evaluates my_list to True if it contains any elements and False if it is empty.

Now, let's run the script to see the output. Open the terminal in VS Code and navigate to the ~/project directory (you should already be in this directory by default). Then, execute the script using the python command:

python list_check.py

You should see the following output:

The list is not empty.

This confirms that our script correctly identifies a non-empty list using boolean evaluation.

Now, let's modify the script to use an empty list and see what happens. Change the my_list variable to an empty list:

## ~/project/list_check.py
my_list = []

if my_list:
    print("The list is not empty.")
else:
    print("The list is empty.")

Run the script again:

python list_check.py

This time, you should see the following output:

The list is empty.

This demonstrates that an empty list is correctly identified as empty using boolean evaluation.

Using boolean evaluation is a concise and Pythonic way to check if a list is non-empty. It is often preferred for its readability and simplicity. This approach is widely used in Python code.

Summary

In this lab, you explored how to check if a list is not empty in Python. You learned that a non-empty list contains at least one item, while an empty list contains no items.

You created a Python script to demonstrate that a non-empty list evaluates to True in a boolean context, resulting in the output "The list is not empty." Conversely, when the list was modified to be empty, it evaluated to False, and the script output "The list is empty." This showcased how Python's boolean evaluation can be used to effectively determine if a list is empty or not.