2/4/25, 3:37 PM Working with Lists - Lab Answersheet - Colab
keyboard_arrow_down Working with Lists - Lab
Introduction
Now that we have a sense of how to read from a list and alter a list in Python, let's put this knowledge to use.
Objectives
You will be able to:
Use indexing to access elements in a list
Apply list methods to make changes to a list
Change elements of a list
Instructions
In the previous lesson, we had a list of top travel cities.
top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Se
Remember to press shift+enter to run each gray block of code (including the one above). Otherwise, the variables will not be
defined.
In this lab we will work with a list of associated countries corresponding to each of the top travel cities.
countries = ['Croatia',
'USA',
'Argentina',
'Mexico',
'USA',
'Morocco',
'New Mexico',
'Finland',
'Argentina',
'Italy',
'Canada',
'South Korea']
Run the code in the cell above by pressing shift + enter.
The list of countries associated with each city has been assigned to the variable countries . Now we will work with this list.
keyboard_arrow_down Accessing elements from lists
First, set the variable italy to be equal to the third to last element from countries .
Note: If you see an error stating that countries is undefined, it means you must press shift+enter in the second gray box
where countries variable is assigned.
italy = None # 'Italy'
italy = countries [3:]
We assigned the varible italy equal to None , but you should change the word None to code that uses the countries list to
assign italy to 'Italy' . We wrote the variable italy a second time, so that you can see what it contains when you run the
code block. Currently, nothing is displayed below as it equals None , but when it's correct it will match the string which is
commented out, 'Italy' .
italy # 'Italy'
print(italy)
['Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']
Now access the fourth element and set it equal to the variable mexico .
https://colab.research.google.com/drive/1UnlBqi7oXSPuSp90fFdwXMgLDJe565XU#printMode=true 1/3
2/4/25, 3:37 PM Working with Lists - Lab Answersheet - Colab
mexico = None
mexico = italy[4]
print(mexico)
Finland
Notice that the second through fifth elements are all in a row and all in the Western Hemisphere. Assign that subset of elements to a variable
called kindof_neighbors .
kindof_neighbors = None
kindof_neighbors = italy [2:6]
print(kindof_neighbors)
['Morocco', 'New Mexico', 'Finland', 'Argentina']
keyboard_arrow_down Changing Elements
Ok, now let's add a couple of countries onto this list. At the end of the list, add the country 'Malta'.
None # add code here
print(countries)
countries.append('Malta')
print(countries)
['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']
['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea',
Then add the country 'Thailand'.
None # add code here
countries.append('Thailand')
print(countries)
['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea',
Now your list of countries should look like the following.
countries
# ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland',
# 'Argentina', 'Italy', 'Canada', 'South Korea', 'Malta', 'Thailand']
print(countries)
['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea',
You may have noticed that "New Mexico" is included in our list of countries. That doesn't seem right. Let's change 'New Mexico' to 'USA'.
None # add code here
countries[6]='USA'
print(countries)
['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'USA', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea', 'Malta
countries
# ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'USA', 'Finland',
# 'Argentina', 'Italy', 'Canada', 'South Korea', 'Malta', 'Thailand']
['Croatia',
'USA',
'Argentina',
'Mexico',
'USA',
'Morocco',
'USA',
'Finland',
'Argentina',
'Italy',
'Canada',
'South Korea',
'Malta',
'Thailand']
https://colab.research.google.com/drive/1UnlBqi7oXSPuSp90fFdwXMgLDJe565XU#printMode=true 2/3
2/4/25, 3:37 PM Working with Lists - Lab Answersheet - Colab
Finally, let's remove Thailand from the list. No good reason, we're acting on whimsy.
countries.pop() # 'Thailand'
countries.pop()
print(countries)
['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'USA', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']
print(countries)
['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'USA', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']
keyboard_arrow_down Exploring Lists with Methods
Ok, now we notice that some countries are mentioned more than once. Let's see how many repeat countries are on this list.
First, use the set and list functions to return a unique list of countries. Set this list equal to the variable unique_countries .
unique_countries = None
unique_countries = list(set(countries))
unique_countries # ['Canada', 'Italy', 'USA', 'Mexico', 'Finland',
#'Malta', 'Morocco', 'Croatia', 'Argentina', 'South Korea']
print(unique_countries)
['Mexico', 'USA', 'Morocco', 'Canada', 'South Korea', 'Finland', 'Argentina', 'Croatia', 'Italy']
Now the number of repeat countries should be the number of countries minus the number of unique countries. So use the len function on
both unique_countries and countries to calculate this and assign the result to the variable num_of_repeats .
num_of_repeats = None
num_of_repeats # 3
num_of_repeats = len(unique_countries)
print(num_of_repeats)
Summary
In this lesson, we practiced working with lists in Python. We saw how to index lists to select specific elements, how to use list methods to
change lists, and how to add and remove elements from a list. Finally, we saw how to use a set to calculate the number of unique elements in
the list.
https://colab.research.google.com/drive/1UnlBqi7oXSPuSp90fFdwXMgLDJe565XU#printMode=true 3/3