Introduction
In this lab, you'll learn how to check if a list is empty in Python. The lab begins by introducing the concept of empty lists and their importance in programming. You'll create an empty list using square brackets and print it to verify its content.
The lab then demonstrates how empty lists can be used to initialize variables and as a starting point for loops and conditional statements. You'll create a script that iterates through a list of numbers and adds even numbers to an initially empty list, showcasing a practical application of empty lists.
Learn About Empty Lists
In this step, you'll learn about empty lists in Python. An empty list is a list that contains no elements. It's like an empty container, ready to be filled with data. Understanding empty lists is crucial because they are often used as starting points for building more complex data structures or as default values in functions.
Let's start by creating an empty list in Python. You can do this using square brackets [] without any elements inside.
Open your VS Code editor in the LabEx environment.
Create a new file named
empty_list.pyin the~/projectdirectory.~/project/empty_list.pyAdd the following code to the
empty_list.pyfile:## Creating an empty list my_list = [] ## Printing the list to see its content print(my_list)Save the file.
Now, run the script using the
pythoncommand in the terminal:python ~/project/empty_list.pyYou should see the following output:
[]This confirms that you have successfully created an empty list.
Empty lists are useful for initializing variables or as a starting point for loops and conditional statements. For example, you might want to create an empty list to store the results of a calculation or to hold items that meet a certain condition.
Here's another example to illustrate the use of an empty list:
## Initialize an empty list
results = []
## Simulate some data
data = [1, 2, 3, 4, 5]
## Iterate through the data and add even numbers to the results list
for number in data:
if number % 2 == 0:
results.append(number)
## Print the results
print(results)
Save this code to even_numbers.py in your ~/project directory and run it:
python ~/project/even_numbers.py
The output will be:
[2, 4]
In this example, we started with an empty list called results and then populated it with even numbers from the data list. This demonstrates how empty lists can be dynamically filled with data based on certain conditions.
Use len() to Check
In this step, you'll learn how to use the len() function to check the length of a list, including empty lists. The len() function is a built-in Python function that returns the number of items in a list (or any other iterable object).
Open your VS Code editor.
Create a new file named
list_length.pyin the~/projectdirectory.~/project/list_length.pyAdd the following code to the
list_length.pyfile:## Creating an empty list my_list = [] ## Checking the length of the empty list list_length = len(my_list) ## Printing the length print(list_length)Save the file.
Run the script using the
pythoncommand in the terminal:python ~/project/list_length.pyYou should see the following output:
0This indicates that the length of the empty list is 0.
The len() function is not limited to empty lists. You can use it to find the length of any list, regardless of its contents. Let's try it with a list that contains some elements:
## Creating a list with elements
my_list = [1, 2, 3, 4, 5]
## Checking the length of the list
list_length = len(my_list)
## Printing the length
print(list_length)
Save this code to list_length_with_elements.py in your ~/project directory and run it:
python ~/project/list_length_with_elements.py
The output will be:
5
This shows that the len() function correctly returns the number of elements in the list.
Understanding how to use len() is important for many programming tasks, such as validating input, controlling loops, and manipulating data structures.
Evaluate as Boolean
In this step, you'll learn how an empty list evaluates as a boolean value in Python. In Python, certain values are considered "truthy" (evaluate to True in a boolean context) and others are considered "falsy" (evaluate to False). An empty list is considered a "falsy" value.
Open your VS Code editor.
Create a new file named
boolean_evaluation.pyin the~/projectdirectory.~/project/boolean_evaluation.pyAdd the following code to the
boolean_evaluation.pyfile:## Creating an empty list my_list = [] ## Checking if the empty list evaluates to True or False if my_list: print("The list is True") else: print("The list is False")Save the file.
Run the script using the
pythoncommand in the terminal:python ~/project/boolean_evaluation.pyYou should see the following output:
The list is FalseThis confirms that an empty list evaluates to
Falsein a boolean context.
This behavior is useful in conditional statements. You can check if a list is empty without explicitly comparing its length to zero.
Here's another example to illustrate this:
## Creating a non-empty list
my_list = [1, 2, 3]
## Checking if the non-empty list evaluates to True or False
if my_list:
print("The list is True")
else:
print("The list is False")
Save this code to boolean_evaluation_non_empty.py in your ~/project directory and run it:
python ~/project/boolean_evaluation_non_empty.py
The output will be:
The list is True
This shows that a non-empty list evaluates to True.
Understanding how lists evaluate as boolean values can help you write more concise and readable code.
Summary
In this lab, you learned about empty lists in Python, which are lists containing no elements and are often used as starting points for data structures or default function values. You created an empty list using square brackets [] and verified its emptiness by printing it to the console, observing the output [].
Furthermore, you explored the practical application of empty lists by initializing one named results and populating it with even numbers from a sample dataset. This demonstrated how empty lists can serve as accumulators within loops and conditional statements, ultimately storing filtered or processed data.



