Chapter 3: Introducing Lists
|
Source: Python Crash Course, 3rd Edition by Eric Matthes |
In this chapter and the next you’ll learn what lists are and how to start working with the elements in a list. Lists allow you to store sets of information in one place, whether you have just a few items or millions of items. Lists are one of Python’s most powerful features readily accessible to new programmers, and they tie together many important concepts in programming.
What Is a List?
A list is a collection of items in a particular order. You can make a
list that includes the letters of the alphabet, the digits from 0 to 9,
or the names of all the people in your family. You can put anything you
want into a list, and the items in your list don’t have to be related in
any particular way. Because a list usually contains more than one
element, it’s a good idea to make the name of your list plural, such as
letters, digits, or names.
In Python, square brackets ([]) indicate a list, and individual
elements in the list are separated by commas. Here’s a simple example of
a list that contains a few kinds of bicycles:
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
If you ask Python to print a list, Python returns its representation of the list, including the square brackets:
['trek', 'cannondale', 'redline', 'specialized']
Because this isn’t the output you want your users to see, let’s learn how to access the individual items in a list.
Accessing Elements in a List
Lists are ordered collections, so you can access any element in a list by telling Python the position, or index, of the item desired. To access an element in a list, write the name of the list followed by the index of the item enclosed in square brackets.
For example, let’s pull out the first bicycle in the list bicycles:
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
When we ask for a single item from a list, Python returns just that element without square brackets:
trek
This is the result you want your users to see: clean, neatly formatted output.
You can also use the string methods from Chapter 2 on any element in a
list. For example, you can format the element 'trek' using the
title() method:
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0].title())
This produces the same output as the preceding example, except 'Trek'
is capitalized.
Index Positions Start at 0, Not 1
Python considers the first item in a list to be at position 0, not position 1. This is true of most programming languages, and the reason has to do with how list operations are implemented at a lower level. If you’re receiving unexpected results, ask yourself if you’re making a simple but common off-by-one error.
The second item in a list has an index of 1. Using this counting system, you can get any element you want from a list by subtracting one from its position in the list. For instance, to access the fourth item in a list, you request the item at index 3.
The following asks for the bicycles at index 1 and index 3:
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[1])
print(bicycles[3])
This code returns the second and fourth bicycles in the list:
cannondale
specialized
Python has a special syntax for accessing the last element in a list. If
you ask for the item at index -1, Python always returns the last item
in the list:
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])
This code returns the value 'specialized'. This syntax is quite
useful, because you’ll often want to access the last items in a list
without knowing exactly how long the list is. This convention extends to
other negative index values as well. The index -2 returns the second
item from the end of the list, the index -3 returns the third item
from the end, and so forth.
Using Individual Values from a List
You can use individual values from a list just as you would any other variable. For example, you can use f-strings to create a message based on a value from a list.
Let’s try pulling the first bicycle from the list and composing a message using that value:
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = f"My first bicycle was a {bicycles[0].title()}."
print(message)
We build a sentence using the value at bicycles[0] and assign it to
the variable message. The output is a simple sentence about the first
bicycle in the list:
My first bicycle was a Trek.
Try It Yourself
Try these short programs to get some firsthand experience with Python’s lists. You might want to create a new folder for each chapter’s exercises to keep them organized.
3-1. Names: Store the names of a few of your friends in a list called
names. Print each person’s name by accessing each element in the list,
one at a time.
3-2. Greetings: Start with the list you used in Exercise 3-1, but instead of just printing each person’s name, print a message to them. The text of each message should be the same, but each message should be personalized with the person’s name.
3-3. Your Own List: Think of your favorite mode of transportation,
such as a motorcycle or a car, and make a list that stores several
examples. Use your list to print a series of statements about these
items, such as I would like to own a Honda motorcycle.
Modifying, Adding, and Removing Elements
Most lists you create will be dynamic, meaning you’ll build a list and then add and remove elements from it as your program runs its course. For example, you might create a game in which a player has to shoot aliens out of the sky. You could store the initial set of aliens in a list and then remove an alien from the list each time one is shot down. Each time a new alien appears on the screen, you add it to the list. Your list of aliens will increase and decrease in length throughout the course of the game.
Modifying Elements in a List
The syntax for modifying an element is similar to the syntax for accessing an element in a list. To change an element, use the name of the list followed by the index of the element you want to change, and then provide the new value you want that item to have.
For example, say we have a list of motorcycles and the first item in the
list is 'honda'. We can change the value of this first item after the
list has been created:
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
Here we define the list motorcycles, with 'honda' as the first
element. Then we change the value of the first item to 'ducati'. The
output shows that the first item has been changed, while the rest of the
list stays the same:
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']
You can change the value of any item in a list, not just the first item.
Adding Elements to a List
Python provides several ways to add new data to existing lists.
Appending Elements to the End of a List
The simplest way to add a new element to a list is to append the item
to the list. When you append an item to a list, the new element is added
to the end of the list. Using the same list we had in the previous
example, we’ll add the new element 'ducati' to the end of the list:
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
Here the append() method adds 'ducati' to the end of the list,
without affecting any of the other elements in the list:
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']
The append() method makes it easy to build lists dynamically. For
example, you can start with an empty list and then add items to the list
using a series of append() calls:
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
The resulting list looks exactly the same as the lists in the previous examples:
['honda', 'yamaha', 'suzuki']
Building lists this way is very common, because you often won’t know the data your users want to store in a program until after the program is running. To put your users in control, start by defining an empty list that will hold the users' values. Then append each new value provided to the list you just created.
Inserting Elements into a List
You can add a new element at any position in your list by using the
insert() method. You do this by specifying the index of the new
element and the value of the new item:
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
In this example, we insert the value 'ducati' at the beginning of the
list. The insert() method opens a space at position 0 and stores the
value 'ducati' there:
['ducati', 'honda', 'yamaha', 'suzuki']
This operation shifts every other value in the list one position to the right.
Removing Elements from a List
Often, you’ll want to remove an item or a set of items from a list. Python gives you several ways to do this, depending on whether you know the item’s position or value, and whether you need to use the item after removing it.
Removing an Item Using the del Statement
If you know the position of the item you want to remove from a list, you
can use the del statement:
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
Here we use the del statement to remove the first item, 'honda',
from the list of motorcycles:
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
You can remove an item from any position in a list using the del
statement if you know its index. For example, here’s how to remove the
second item, 'yamaha':
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[1]
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'suzuki']
In both examples, you can no longer access the value that was removed
from the list after the del statement is used.
Removing an Item Using the pop() Method
Sometimes you’ll want to use the value of an item after you remove it from a list. For example, you might want to get the x and y position of an alien that was just shot down, so you can draw an explosion at that position. In a web application, you might want to remove a user from a list of active members and then add that user to a list of inactive members.
The pop() method removes the last item in a list, but it lets you work
with that item after removing it. The term pop comes from thinking of
a list as a stack of items and popping one item off the top of the stack.
In this analogy, the top of a stack corresponds to the end of a list.
Let’s pop a motorcycle from the list of motorcycles:
motorcycles = ['honda', 'yamaha', 'suzuki'] (1)
print(motorcycles)
popped_motorcycle = motorcycles.pop() (2)
print(motorcycles) (3)
print(popped_motorcycle) (4)
| 1 | Define and print the list motorcycles. |
| 2 | Pop a value from the list and assign it to popped_motorcycle. |
| 3 | Print the list to show that a value has been removed. |
| 4 | Print the popped value to prove we still have access to it. |
The output shows that the value 'suzuki' was removed from the end of
the list and is now assigned to the variable popped_motorcycle:
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki
How might this pop() method be useful? Imagine that the motorcycles in
the list are stored in chronological order according to when we owned
them. We can use pop() to print a statement about the last motorcycle
we bought:
motorcycles = ['honda', 'yamaha', 'suzuki']
last_owned = motorcycles.pop()
print(f"The last motorcycle I owned was a {last_owned.title()}.")
The last motorcycle I owned was a Suzuki.
Popping Items from Any Position in a List
You can use pop() to remove an item from any position in a list by
including the index of the item you want to remove in parentheses:
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print(f"The first motorcycle I owned was a {first_owned.title()}.")
The first motorcycle I owned was a Honda.
Remember that each time you use pop(), the item you work with is no
longer stored in the list.
If you’re unsure whether to use the del statement or the pop()
method, here’s a simple way to decide: when you want to delete an item
from a list and not use that item in any way, use the del statement;
if you want to use an item as you remove it, use the pop() method.
Removing an Item by Value
Sometimes you won’t know the position of the value you want to remove
from a list. If you only know the value of the item you want to remove,
you can use the remove() method.
For example, say we want to remove the value 'ducati' from the list of
motorcycles:
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
Here the remove() method tells Python to figure out where 'ducati'
appears in the list and remove that element:
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
You can also use the remove() method to work with a value that’s being
removed from a list. Let’s remove the value 'ducati' and print a
reason for removing it:
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] (1)
print(motorcycles)
too_expensive = 'ducati' (2)
motorcycles.remove(too_expensive) (3)
print(motorcycles)
print(f"A {too_expensive.title()} is too expensive for me.") (4)
| 1 | Define the list. |
| 2 | Assign the value 'ducati' to a variable called too_expensive. |
| 3 | Use the variable to tell Python which value to remove from the list. |
| 4 | The value 'ducati' has been removed from the list but is still
accessible through the variable too_expensive. |
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
A Ducati is too expensive for me.
|
The |
Try It Yourself
The following exercises are a bit more complex than those in Chapter 2, but they give you an opportunity to use lists in all of the ways described.
3-4. Guest List: If you could invite anyone, living or deceased, to dinner, who would you invite? Make a list that includes at least three people you’d like to invite to dinner. Then use your list to print a message to each person, inviting them to dinner.
3-5. Changing Guest List: You just heard that one of your guests can’t
make the dinner, so you need to send out a new set of invitations.
You’ll have to think of someone else to invite. Start with your program
from Exercise 3-4. Add a print() call at the end stating the name of
the guest who can’t make it. Modify your list, replacing the name of the
guest who can’t make it with the name of the new person you are
inviting. Print a second set of invitation messages, one for each person
who is still in your list.
3-6. More Guests: You just found a bigger dinner table, so now more
space is available. Think of three more guests to invite to dinner.
Start with your program from Exercise 3-4 or 3-5. Add a print() call
informing people that you found a bigger table. Use insert() to add
one new guest to the beginning of your list. Use insert() to add one
new guest to the middle of your list. Use append() to add one new
guest to the end of your list. Print a new set of invitation messages,
one for each person in your list.
3-7. Shrinking Guest List: You just found out that your new dinner
table won’t arrive in time for the dinner, and now you have space for
only two guests. Start with your program from Exercise 3-6. Add a new
line that prints a message saying that you can invite only two people
for dinner. Use pop() to remove guests from your list one at a time
until only two names remain. Each time you pop a name from your list,
print a message to that person letting them know you’re sorry you can’t
invite them to dinner. Print a message to each of the two people still
on your list, letting them know they’re still invited. Use del to
remove the last two names from your list, so you have an empty list.
Print your list to make sure you actually have an empty list at the end
of your program.
Organizing a List
Often, your lists will be created in an unpredictable order, because you can’t always control the order in which your users provide their data. Although this is unavoidable in most circumstances, you’ll frequently want to present your information in a particular order. Python provides a number of different ways to organize your lists, depending on the situation.
Sorting a List Permanently with the sort() Method
Python’s sort() method makes it relatively easy to sort a list.
Imagine we have a list of cars and want to change the order of the list
to store them alphabetically. To keep the task simple, let’s assume that
all the values in the list are lowercase:
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
The sort() method changes the order of the list permanently. The cars
are now in alphabetical order, and we can never revert to the original
order:
['audi', 'bmw', 'subaru', 'toyota']
You can also sort this list in reverse-alphabetical order by passing the
argument reverse=True to the sort() method:
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)
Again, the order of the list is permanently changed:
['toyota', 'subaru', 'bmw', 'audi']
Sorting a List Temporarily with the sorted() Function
To maintain the original order of a list but present it in a sorted
order, you can use the sorted() function. The sorted() function lets
you display your list in a particular order, but doesn’t affect the
actual order of the list.
Let’s try this function on the list of cars:
cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:") (1)
print(cars)
print("Here is the sorted list:") (2)
print(sorted(cars))
print("Here is the original list again:") (3)
print(cars)
| 1 | Print the list in its original order. |
| 2 | Print the list in alphabetical order using sorted(). |
| 3 | After the list is displayed in the new order, show that the list is still stored in its original order. |
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']
Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']
Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']
Notice that the list still exists in its original order after the
sorted() function has been used. The sorted() function can also
accept a reverse=True argument if you want to display a list in
reverse-alphabetical order.
|
Sorting a list alphabetically is a bit more complicated when all the values are not in lowercase. There are several ways to interpret capital letters when determining a sort order, and specifying the exact order can be more complex than we want to deal with at this time. However, most approaches to sorting will build directly on what you learned in this section. |
Printing a List in Reverse Order
To reverse the original order of a list, you can use the reverse()
method. If we originally stored the list of cars in chronological order
according to when we owned them, we could easily rearrange the list into
reverse-chronological order:
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
Notice that reverse() doesn’t sort backward alphabetically; it simply
reverses the order of the list:
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']
The reverse() method changes the order of a list permanently, but you
can revert to the original order anytime by applying reverse() to the
same list a second time.
Finding the Length of a List
You can quickly find the length of a list by using the len() function.
The list in this example has four items, so its length is 4:
>>> cars = ['bmw', 'audi', 'toyota', 'subaru']
>>> len(cars)
4
You’ll find len() useful when you need to identify the number of
aliens that still need to be shot down in a game, determine the amount
of data you have to manage in a visualization, or figure out the number
of registered users on a website, among other tasks.
|
Python counts the items in a list starting with one, so you shouldn’t run into any off-by-one errors when determining the length of a list. |
Try It Yourself
3-8. Seeing the World: Think of at least five places in the world
you’d like to visit. Store the locations in a list. Make sure the list
is not in alphabetical order. Print your list in its original order.
Use sorted() to print your list in alphabetical order without
modifying the actual list. Show that your list is still in its original
order by printing it. Use sorted() to print your list in
reverse-alphabetical order without changing the order of the original
list. Show that your list is still in its original order by printing it
again. Use reverse() to change the order of your list. Print the list
to show that its order has changed. Use reverse() to change the order
of your list again. Print the list to show it’s back to its original
order. Use sort() to change your list so it’s stored in alphabetical
order. Print the list to show that its order has been changed. Use
sort() to change your list so it’s stored in reverse-alphabetical
order. Print the list to show that its order has changed.
3-9. Dinner Guests: Working with one of the programs from Exercises
3-4 through 3-7, use len() to print a message indicating the number
of people you’re inviting to dinner.
3-10. Every Function: Think of things you could store in a list. For example, you could make a list of mountains, rivers, countries, cities, languages, or anything else you’d like. Write a program that creates a list containing these items and then uses each function introduced in this chapter at least once.
Avoiding Index Errors When Working with Lists
There’s one type of error that’s common to see when you’re working with lists for the first time. Let’s say you have a list with three items, and you ask for the fourth item:
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[3])
This example results in an index error:
Traceback (most recent call last):
File "motorcycles.py", line 2, in <module>
print(motorcycles[3])
~~~~~~~~~~~^^^
IndexError: list index out of range
Python attempts to give you the item at index 3. But when it searches
the list, no item in motorcycles has an index of 3. Because of the
off-by-one nature of indexing in lists, this error is typical. People
think the third item is item number 3, because they start counting at 1.
But in Python the third item is number 2, because it starts indexing at 0.
An index error means Python can’t find an item at the index you requested. If an index error occurs in your program, try adjusting the index you’re asking for by one. Then run the program again to see if the results are correct.
Keep in mind that whenever you want to access the last item in a list,
you should use the index -1. This will always work, even if your list
has changed size since the last time you accessed it:
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[-1])
The index -1 always returns the last item in a list, in this case the
value 'suzuki':
suzuki
The only time this approach will cause an error is when you request the last item from an empty list:
motorcycles = []
print(motorcycles[-1])
No items are in motorcycles, so Python returns another index error:
Traceback (most recent call last):
File "motorcycles.py", line 2, in <module>
print(motorcycles[-1])
~~~~~~~~~~~^^^^
IndexError: list index out of range
If an index error occurs and you can’t figure out how to resolve it, try printing your list or just printing the length of your list. Your list might look much different than you thought it did, especially if it has been managed dynamically by your program. Seeing the actual list, or the exact number of items in your list, can help you sort out such logical errors.
Try It Yourself
3-11. Intentional Error: If you haven’t received an index error in one of your programs yet, try to make one happen. Change an index in one of your programs to produce an index error. Make sure you correct the error before closing the program.
Summary
In this chapter, you learned what lists are and how to work with the individual items in a list. You learned how to define a list and how to add and remove elements. You learned how to sort lists permanently and temporarily for display purposes. You also learned how to find the length of a list and how to avoid index errors when you’re working with lists.
In Chapter 4 you’ll learn how to work with items in a list more efficiently. By looping through each item in a list using just a few lines of code you’ll be able to work efficiently, even when your list contains thousands or millions of items.
Applied Exercises: Ch 3 — Introducing Lists
These exercises cover the same concepts as the chapter but use context
from real infrastructure, network security, and language learning work.
Save each as a separate .py file using lowercase and underscores, e.g.
ise_nodes.py.
Domus Digitalis / Homelab
D3-1. Node Inventory: Create a list called nodes containing the
hostnames of your KVM hypervisors, VyOS routers, and k3s master node.
Print each hostname by accessing each element in the list, one at a
time.
D3-2. VLAN List: Create a list called vlans containing at least six
VLAN names from your Domus Digitalis architecture (e.g. 'INFRA',
'SECURITY', 'SERVICES'). Print the list in its original order, then
use sorted() to print it in alphabetical order without modifying the
original. Confirm the original order is unchanged by printing it again.
D3-3. Service Queue: Start with an empty list called pending_services.
Use append() to add at least four services you want to deploy to your
k3s cluster. Print the list after all appends. Then use insert() to
add a high-priority service at the beginning of the list. Print the
updated list.
D3-4. Decommission a Node: Create a list called hypervisors
containing several node names. Use pop() to remove the last node and
print a message like: Decommissioning node: kvm-02. Then use
remove() to remove a specific node by name from the remaining list.
Print the final list.
D3-5. Stack Length: Create a list representing your Domus Digitalis
software stack (e.g. VyOS, FreeIPA, Vault, k3s, Wazuh, Gitea, MinIO).
Use len() to print a message like:
Domus Digitalis stack has 7 components.
CHLA / ISE / Network Security
C3-1. ISE Policy Sets: Create a list called policy_sets containing
at least four ISE policy set names. Print each policy set name with a
message like: Policy set active: 802.1X Wired. Use title() on each
element as you print.
C3-2. Endpoint Groups: Create a list called endpoint_groups
containing several ISE endpoint group names. Use sort() to sort the
list alphabetically and print the result. Then use sort(reverse=True)
to sort in reverse order and print again.
C3-3. Log Source Queue: Create a list called log_sources representing
your Monad pipeline inputs (e.g. 'ise-syslog', 'ftd-syslog',
'wazuh'). Use insert() to add a new source at index 0. Use
append() to add another source at the end. Print the full list and
its length.
C3-4. Removing a Decommissioned Source: Start with a list of syslog
sources. Assign the name of a source being retired to a variable called
retired_source. Use remove() to remove it from the list. Print the
updated list and a message explaining why it was removed.
C3-5. Index Error Practice: Create a list of three ISE profiling
policies. Try to access index 3 and observe the IndexError. Then fix
your code to use index -1 to access the last policy correctly. Add a
comment explaining the difference.
General Sysadmin / Linux
L3-1. Service List: Create a list called services containing at
least five systemd service names you manage (e.g. 'wazuh-manager',
'k3s', 'vault'). Print each service with a message like:
Checking status: wazuh-manager.
L3-2. Sorted Packages: Create a list of package names you commonly
install on Rocky Linux. Use sorted() to display them in alphabetical
order without modifying the original list. Confirm the original is
unchanged by printing it after.
L3-3. Reverse Chronological Log: Create a list called recent_events
representing log entries or deployment steps in the order they occurred.
Use reverse() to flip the order to most-recent-first and print the
result. Use reverse() again to restore the original order.
L3-4. Dynamic Interface List: Start with an empty list called
active_interfaces. Use append() to add at least four interface names
as they come online. Print the list. Then use pop() to remove the last
interface and print a message indicating it went offline. Print the final
list and its length.
Spanish / DELE C2
E3-1. Vocabulary List: Create a list called vocabulario with at
least six Spanish words you’ve encountered recently in Don Quijote.
Print each word individually.
E3-2. Sorted Vocabulary: Use the list from Exercise E3-1. Use
sorted() to display the words in alphabetical order without modifying
the original. Then use sort() to permanently sort the list and print
it.
E3-3. Chapter List: Create a list called capitulos_leidos
representing the chapter numbers of Don Quijote you’ve completed.
Use len() to print a message like:
Has completado 30 capítulos de Don Quijote.
E3-4. Study Queue: Create a list called temas_pendientes with at
least four DELE C2 study topics (e.g. 'subjuntivo', 'perífrasis',
'registro formal'). Use insert() to add a high-priority topic at
the beginning. Use pop() to remove the last topic when it’s complete
and print a message like: Tema completado: registro formal. Print the
remaining list.
E3-5. Verb Forms: Create a list called conjugaciones containing
several conjugated forms of a Spanish verb (e.g. forms of haber).
Use reverse() to flip the order. Print the reversed list. Then use
reverse() again to restore the original order and confirm it’s back.