-{"cells": [{"cell_type": "markdown", "metadata": {"collapsed": true}, "source": ["# Lists Lab "]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Introduction"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Ok, so now that we have a sense of how to read from and alter a list in Python, let's put this knowledge to use. "]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Objectives"]}, {"cell_type": "markdown", "metadata": {}, "source": ["* Practice reading one and multiple elements from lists\n", "* Practice altering data in lists\n", "* Practice adding elements and removing elements from lists"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Our initial data structure "]}, {"cell_type": "markdown", "metadata": {}, "source": ["In the previous lesson, we had a list of top travel cities."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']"]}, {"cell_type": "markdown", "metadata": {}, "source": ["> Remember to press shift+enter to run each gray block of code (including the one above). Otherwise, the variables will not be defined."]}, {"cell_type": "markdown", "metadata": {}, "source": ["In this lesson we will work with a list of associated countries corresponding to each of the top travel cities."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["countries = ['Croatia',\n", " 'USA',\n", " 'Argentina',\n", " 'Mexico',\n", " 'USA',\n", " 'Morocco',\n", " 'New Mexico',\n", " 'Finland',\n", " 'Argentina',\n", " 'Italy',\n", " 'Canada',\n", " 'South Korea']"]}, {"cell_type": "markdown", "metadata": {}, "source": ["> Run the code in the cell above by pressing shift + enter."]}, {"cell_type": "markdown", "metadata": {}, "source": ["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."]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Accessing elements from lists"]}, {"cell_type": "markdown", "metadata": {}, "source": ["First, set the variable `italy` to be equal to the third to last element from `countries`. \n", ">**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."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["italy = None # 'Italy'\n", "italy"]}, {"cell_type": "markdown", "metadata": {}, "source": ["> We assign 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 equals 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'`."]}, {"cell_type": "code", "execution_count": null, "metadata": {"scrolled": true}, "outputs": [], "source": ["italy # 'Italy'"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Now access the fourth element and set it equal to the variable `mexico`."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["mexico = None\n", "mexico"]}, {"cell_type": "markdown", "metadata": {}, "source": ["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`."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["kindof_neighbors = None\n", "kindof_neighbors"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Changing Elements"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Ok, now let's add a couple of countries onto this list. At the end of the list, add the country 'Malta'."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["None # add code here"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Then add the country 'Thailand'."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["None # add code here"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Now your list of countries should look like the following."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["countries \n", "# ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', \n", "# 'Argentina', 'Italy', 'Canada', 'South Korea', 'Malta', 'Thailand']"]}, {"cell_type": "markdown", "metadata": {}, "source": ["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'."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["countries = None # add code here"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["countries \n", "# ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'USA', 'Finland', \n", "# 'Argentina', 'Italy', 'Canada', 'South Korea', 'Malta', 'Thailand']"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Finally, let's remove Thailand from the list. No good reason, we're acting on whimsy."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["countries = ['Croatia',\n", " 'USA',\n", " 'Argentina',\n", " 'Mexico',\n", " 'USA',\n", " 'Morocco',\n", " 'USA',\n", " 'Finland',\n", " 'Argentina',\n", " 'Italy',\n", " 'Canada',\n", " 'South Korea', \n", " 'Malta', \n", " 'Thailand']\n", "countries.pop() # 'Thailand'\n", "countries\n", "# ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'USA', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea', 'Malta']"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Exploring Lists with Methods"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Ok, now we notice that some countries are mentioned more than once. Let's see how many repeat countries are on this list. "]}, {"cell_type": "markdown", "metadata": {}, "source": ["First, use the `set` and `list` functions to return a unique list of countries. Set this list equal to the variable `unique_countries`."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["unique_countries = None"]}, {"cell_type": "code", "execution_count": null, "metadata": {"scrolled": true}, "outputs": [], "source": ["unique_countries # ['Croatia', 'Argentina', 'Canada', 'Mexico', 'Italy', \n", "#'South Korea', 'USA', 'Morocco', 'Malta', 'Finland'] Note: order of countries may be different"]}, {"cell_type": "markdown", "metadata": {}, "source": ["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`."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["num_of_repeats = None\n", "num_of_repeats # 3"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Summary"]}, {"cell_type": "markdown", "metadata": {"collapsed": true}, "source": ["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 of unique elements in the list."]}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3"}}, "nbformat": 4, "nbformat_minor": 2}
0 commit comments