Ok, so 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.
- Practice reading one and multiple elements from lists
- Practice altering data in lists
- Practice adding elements and removing elements from lists
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 Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']Remember to press shift+enter to run each gray block of code (including the one above). Otherwise, the variables will not be defined.
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.
Ok, so the list of countries associated with each city has been assigned to the variable countries. Now we will work with reading and manipulating this list.
For the tests in this lab to work, please run the following two cells.
!pip install ipython_unittestCollecting ipython_unittest
Downloading ipython_unittest-0.3.1-py2.py3-none-any.whl
Installing collected packages: ipython-unittest
Successfully installed ipython-unittest-0.3.1
%load_ext ipython_unittestFirst, set the variable italy to be equal to the third to last element from countries.
Note: If you see an error stating that
countriesis undefined, it means you must press shift+enter in the second gray box wherecountriesvariable is assigned.
italy = None # 'Italy'
italyWe assign the varible
italyequal toNone, but you should change the wordNoneto code that uses thecountrieslist to assignitalyto'Italy'. We wrote the variableitalya second time, so that you can see what it equals when you run the code block. Currently, nothing is displayed below as it equalsNone, but when it's correct it will match the string which is commented out,'Italy'.
italy # 'Italy'%%unittest_testcase
def test_italy(self):
self.assertEqual(italy, 'Italy')Now access the fourth element and set it equal to the variable mexico.
mexico = None
mexico%%unittest_testcase
def test_mexico(self):
self.assertEqual(mexico, 'Mexico')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%%unittest_testcase
def test_kindof_neighbors(self):
self.assertEqual(kindof_neighbors, ['USA', 'Argentina', 'Mexico', 'USA'])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 hereThen add the country 'Thailand'.
None # add code hereNow 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']%%unittest_testcase
def test_countries(self):
self.assertItemsEqual(countries, ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea', 'Malta', 'Thailand'])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 herecountries
# ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'USA', 'Finland',
# 'Argentina', 'Italy', 'Canada', 'South Korea', 'Malta', 'Thailand']%%unittest_testcase
def test_countries_with_usa(self):
self.assertNotIn('New Mexico', countries)Finally, let's remove Thailand from the list. No good reason, we're acting on whimsy.
countries = ['Croatia',
'USA',
'Argentina',
'Mexico',
'USA',
'Morocco',
'USA',
'Finland',
'Argentina',
'Italy',
'Canada',
'South Korea',
'Malta',
'Thailand']
countries.pop() # 'Thailand'
countries
# ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'USA', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea', 'Malta']['Croatia',
'USA',
'Argentina',
'Mexico',
'USA',
'Morocco',
'USA',
'Finland',
'Argentina',
'Italy',
'Canada',
'South Korea',
'Malta']
%%unittest_testcase
def test_countries_with_usa(self):
self.assertNotIn('Thailand', countries)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 = Noneunique_countries # ['Canada', 'Italy', 'USA', 'Mexico', 'Finland',
#'Malta', 'Morocco', 'Croatia', 'Argentina', 'South Korea']%%unittest_testcase
def test_unique_countries(self):
self.assertItemsEqual(unique_countries, ['USA', 'South Korea', 'Morocco', 'Finland', 'Italy', 'Mexico', 'Argentina', 'Malta', 'Croatia', 'Canada'])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%%unittest_testcase
def test_num_of_repeats(self):
self.assertEqual(num_of_repeats, 3)In this lesson, we had some practice with working with lists in Python. We saw how to add and remove elements from a list, as well as select specific elements. Finally, we saw how to use a different data structure to calculate the number unique elements in the list.