From 8a9c33788ada6d84397b0d3814ef10f9c3c5d741 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:37:48 +0000 Subject: [PATCH 01/25] Automatically backed up by Learn --- index.ipynb | 203 +++++++++++++++++++++++++++++----------------------- 1 file changed, 114 insertions(+), 89 deletions(-) diff --git a/index.ipynb b/index.ipynb index 544f3fe500..7c52d2023f 100644 --- a/index.ipynb +++ b/index.ipynb @@ -96,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -116,7 +116,7 @@ " 'South Korea']" ] }, - "execution_count": 76, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -134,10 +134,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": 6, + "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']" @@ -145,7 +143,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -165,7 +163,7 @@ " 'Pyeongchang']" ] }, - "execution_count": 3, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -176,10 +174,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": 8, + "metadata": {}, "outputs": [], "source": [ "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" @@ -212,7 +208,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -232,7 +228,7 @@ " 'Pyeongchang']" ] }, - "execution_count": 5, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -243,22 +239,28 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'Solta'" + "['Solta', 'Greenville', 'Buenos Aires']" ] }, - "execution_count": 6, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "top_travel_cities[0]" + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" ] }, { @@ -272,7 +274,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -281,7 +283,7 @@ "'Buenos Aires'" ] }, - "execution_count": 82, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -299,7 +301,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -308,7 +310,7 @@ "'Pyeongchang'" ] }, - "execution_count": 83, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -326,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -335,7 +337,7 @@ "'Toronto'" ] }, - "execution_count": 84, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -348,12 +350,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable." + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " ] }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -362,7 +364,7 @@ "'Toronto'" ] }, - "execution_count": 85, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -374,7 +376,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -383,7 +385,7 @@ "str" ] }, - "execution_count": 86, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -401,7 +403,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -421,7 +423,7 @@ " 'Pyeongchang']" ] }, - "execution_count": 7, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -432,7 +434,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -441,7 +443,7 @@ "list" ] }, - "execution_count": 8, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -495,29 +497,19 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 1, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 90, - "metadata": {}, - "output_type": "execute_result" + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] } ], "source": [ @@ -533,7 +525,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -542,13 +534,14 @@ "['Walla Walla Valley']" ] }, - "execution_count": 91, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "top_travel_cities[4:5]" + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" ] }, { @@ -560,7 +553,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -569,7 +562,7 @@ "['Walla Walla Valley', 'Marakesh']" ] }, - "execution_count": 10, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -587,7 +580,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -607,7 +600,7 @@ " 'Pyeongchang']" ] }, - "execution_count": 11, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -667,13 +660,36 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "top_travel_cities.append('San Antonio')" + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" ] }, { @@ -685,7 +701,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -706,7 +722,7 @@ " 'San Antonio']" ] }, - "execution_count": 14, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -724,7 +740,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -742,16 +758,17 @@ " 'Salina Island',\n", " 'Toronto',\n", " 'Pyeongchang',\n", - " 'San Antonio',\n", " 'San Antonio']" ] }, - "execution_count": 15, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", "top_travel_cities.append('San Antonio')\n", "top_travel_cities" ] @@ -765,22 +782,34 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'San Antonio'" + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" ] }, - "execution_count": 16, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "top_travel_cities.pop()" + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" ] }, { @@ -792,7 +821,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -801,7 +830,7 @@ "'Walla Walla Valley'" ] }, - "execution_count": 17, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -812,10 +841,8 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": true - }, + "execution_count": 5, + "metadata": {}, "outputs": [], "source": [ "top_travel_cities[4] = 4" @@ -823,7 +850,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -839,12 +866,10 @@ " 'Archipelago Sea',\n", " 'Iguazu Falls',\n", " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" + " 'Toronto']" ] }, - "execution_count": 19, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -1254,14 +1279,14 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.14" + "pygments_lexer": "ipython3", + "version": "3.6.6" } }, "nbformat": 4, From f5b5760ca8e2e59f5a757cad7048cf229aed4d87 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:40:07 +0000 Subject: [PATCH 02/25] Automatically backed up by Learn --- .~index.ipynb | 1294 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1294 ------------------------------------------------- 2 files changed, 1294 insertions(+), 1294 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..7c52d2023f --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1294 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index 7c52d2023f..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1294 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 9bf661f0dc907e167216b8f229775a9128a7af0b Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:40:13 +0000 Subject: [PATCH 03/25] Automatically backed up by Learn --- .~index.ipynb | 1294 ------------------------------------------------ index.ipynb | 1295 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1295 insertions(+), 1294 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index 7c52d2023f..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1294 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..58437fbc6e 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1295 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\n", + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 14f547ea8899e23ac03ada97f8c44a968d2d98a5 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:40:28 +0000 Subject: [PATCH 04/25] Automatically backed up by Learn --- .~index.ipynb | 1295 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1295 ------------------------------------------------- 2 files changed, 1295 insertions(+), 1295 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..58437fbc6e --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1295 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "\n", + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index 58437fbc6e..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1295 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "\n", - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From e5e02ccafd50681e5903c881f3732c4af3d99c4b Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:40:33 +0000 Subject: [PATCH 05/25] Automatically backed up by Learn --- .~index.ipynb | 1295 ------------------------------------------------- index.ipynb | 1292 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1292 insertions(+), 1295 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index 58437fbc6e..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1295 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "\n", - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..9bfb5c017d 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 3a9ed274d820ab7f4f62cf98d79ea127ac5e41ae Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:40:48 +0000 Subject: [PATCH 06/25] Automatically backed up by Learn --- .~index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1292 ------------------------------------------------- 2 files changed, 1292 insertions(+), 1292 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..9bfb5c017d --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index 9bfb5c017d..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 364b55e6e84750616216c84645eee5feff0449dd Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:40:53 +0000 Subject: [PATCH 07/25] Automatically backed up by Learn --- .~index.ipynb | 1292 ------------------------------------------------- index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1292 insertions(+), 1292 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index 9bfb5c017d..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..8846eeb28b 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 0cbbce7405cc4ef8c83ebb91ce41456816534506 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:41:07 +0000 Subject: [PATCH 08/25] Automatically backed up by Learn --- .~index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1292 ------------------------------------------------- 2 files changed, 1292 insertions(+), 1292 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..8846eeb28b --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index 8846eeb28b..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From a506c9bcaa637689cd1b47a039a162fc75af0838 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:41:13 +0000 Subject: [PATCH 09/25] Automatically backed up by Learn --- .~index.ipynb | 1292 ------------------------------------------------- index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1292 insertions(+), 1292 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index 8846eeb28b..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m-------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..2ab155c906 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 475e7d6f82967daa03daa31ccfc0088781340843 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:41:28 +0000 Subject: [PATCH 10/25] Automatically backed up by Learn --- .~index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1292 ------------------------------------------------- 2 files changed, 1292 insertions(+), 1292 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..2ab155c906 --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index 2ab155c906..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 0adecc7d8bd403f1eaf592c66c91cb1af0f374d2 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:41:33 +0000 Subject: [PATCH 11/25] Automatically backed up by Learn --- .~index.ipynb | 1292 ------------------------------------------------- index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1292 insertions(+), 1292 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index 2ab155c906..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..60644e0986 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 5b5f8a3afb3a64066608ced6c5313967d5ff47e6 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:41:48 +0000 Subject: [PATCH 12/25] Automatically backed up by Learn --- .~index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1292 ------------------------------------------------- 2 files changed, 1292 insertions(+), 1292 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..60644e0986 --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index 60644e0986..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 8151c43b9bc387eafcf7e5d40899463832da991a Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:41:52 +0000 Subject: [PATCH 13/25] Automatically backed up by Learn --- .~index.ipynb | 1292 ------------------------------------------------- index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1292 insertions(+), 1292 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index 60644e0986..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..1747e65e17 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities_list = unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From c0c965057f73b62a1b488127957f59c8a629487e Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:42:08 +0000 Subject: [PATCH 14/25] Automatically backed up by Learn --- .~index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1292 ------------------------------------------------- 2 files changed, 1292 insertions(+), 1292 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..1747e65e17 --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'set' object does not support indexing", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" + ] + } + ], + "source": [ + "unique_travel_cities_list = unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index 1747e65e17..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities_list = unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 68bbd98a83bf198d214abd256a80b7c72dd1fd0c Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:42:12 +0000 Subject: [PATCH 15/25] Automatically backed up by Learn --- .~index.ipynb | 1292 ------------------------------------------------- index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1292 insertions(+), 1292 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index 1747e65e17..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'set' object does not support indexing", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" - ] - } - ], - "source": [ - "unique_travel_cities_list = unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..90dafc2186 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'unique_travel_cities_list' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0munique_travel_cities\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'unique_travel_cities_list' is not defined" + ] + } + ], + "source": [ + "unique_travel_cities_list.list(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From d8befafe3826486c1df69930d49ce29fd858c367 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:42:28 +0000 Subject: [PATCH 16/25] Automatically backed up by Learn --- .~index.ipynb | 1292 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1292 ------------------------------------------------- 2 files changed, 1292 insertions(+), 1292 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..90dafc2186 --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'unique_travel_cities_list' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0munique_travel_cities\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'unique_travel_cities_list' is not defined" + ] + } + ], + "source": [ + "unique_travel_cities_list.list(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index 90dafc2186..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'unique_travel_cities_list' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0munique_travel_cities\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'unique_travel_cities_list' is not defined" - ] - } - ], - "source": [ - "unique_travel_cities_list.list(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From af827d84b5d698ea75e632c60f7d627a8e8fc1c4 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:42:33 +0000 Subject: [PATCH 17/25] Automatically backed up by Learn --- .~index.ipynb | 1292 ------------------------------------------------- index.ipynb | 1282 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1282 insertions(+), 1292 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index 90dafc2186..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1292 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'unique_travel_cities_list' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0munique_travel_cities_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0munique_travel_cities\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'unique_travel_cities_list' is not defined" - ] - } - ], - "source": [ - "unique_travel_cities_list.list(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..b312c97b73 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1282 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "unique_travel_cities_list = list(unique_travel_cities)\n", + "\n", + "uniq" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From e0360a6434fb4eb144c3cf307f8a7f5d6970bbdb Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:42:48 +0000 Subject: [PATCH 18/25] Automatically backed up by Learn --- .~index.ipynb | 1282 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1282 ------------------------------------------------- 2 files changed, 1282 insertions(+), 1282 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..b312c97b73 --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1282 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "unique_travel_cities_list = list(unique_travel_cities)\n", + "\n", + "uniq" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index b312c97b73..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1282 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "unique_travel_cities_list = list(unique_travel_cities)\n", - "\n", - "uniq" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 31d39289e09bb56ab5b12785af35832541338ad1 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:42:53 +0000 Subject: [PATCH 19/25] Automatically backed up by Learn --- .~index.ipynb | 1282 ------------------------------------------------ index.ipynb | 1305 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1305 insertions(+), 1282 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index b312c97b73..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1282 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "unique_travel_cities_list = list(unique_travel_cities)\n", - "\n", - "uniq" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..f67ca4d653 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1305 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Iguazu Falls',\n", + " 'Solta',\n", + " 'Archipelago Sea',\n", + " 'San Antonio',\n", + " 'Albuquerque',\n", + " 'Walla Walla Valley',\n", + " 'Greenville',\n", + " 'Toronto',\n", + " 'Marakesh',\n", + " 'Salina Island',\n", + " 'Buenos Aires',\n", + " 'Pyeongchang',\n", + " 'Los Cabos']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities_list = list(unique_travel_cities)\n", + "\n", + "unique_travel_cities_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 1b6eba9f4c818cfa1dfe27feac4120c3a91f6e33 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:43:07 +0000 Subject: [PATCH 20/25] Automatically backed up by Learn --- .~index.ipynb | 1305 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 24 +- 2 files changed, 1311 insertions(+), 18 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..f67ca4d653 --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1305 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Iguazu Falls',\n", + " 'Solta',\n", + " 'Archipelago Sea',\n", + " 'San Antonio',\n", + " 'Albuquerque',\n", + " 'Walla Walla Valley',\n", + " 'Greenville',\n", + " 'Toronto',\n", + " 'Marakesh',\n", + " 'Salina Island',\n", + " 'Buenos Aires',\n", + " 'Pyeongchang',\n", + " 'Los Cabos']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities_list = list(unique_travel_cities)\n", + "\n", + "unique_travel_cities_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index f67ca4d653..acdafe8f6b 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1041,28 +1041,16 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['Iguazu Falls',\n", - " 'Solta',\n", - " 'Archipelago Sea',\n", - " 'San Antonio',\n", - " 'Albuquerque',\n", - " 'Walla Walla Valley',\n", - " 'Greenville',\n", - " 'Toronto',\n", - " 'Marakesh',\n", - " 'Salina Island',\n", - " 'Buenos Aires',\n", - " 'Pyeongchang',\n", - " 'Los Cabos']" + "'Solta'" ] }, - "execution_count": 11, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -1070,7 +1058,7 @@ "source": [ "unique_travel_cities_list = list(unique_travel_cities)\n", "\n", - "unique_travel_cities_list" + "unique_travel_cities_list[1]" ] }, { @@ -1082,7 +1070,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -1091,7 +1079,7 @@ "set" ] }, - "execution_count": 27, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } From 01d2cb9a43dad469cf94fc5a09a93946f3a0ca27 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:43:12 +0000 Subject: [PATCH 21/25] Automatically backed up by Learn --- .~index.ipynb | 1305 ------------------------------------------------- 1 file changed, 1305 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index f67ca4d653..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1305 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Iguazu Falls',\n", - " 'Solta',\n", - " 'Archipelago Sea',\n", - " 'San Antonio',\n", - " 'Albuquerque',\n", - " 'Walla Walla Valley',\n", - " 'Greenville',\n", - " 'Toronto',\n", - " 'Marakesh',\n", - " 'Salina Island',\n", - " 'Buenos Aires',\n", - " 'Pyeongchang',\n", - " 'Los Cabos']" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities_list = list(unique_travel_cities)\n", - "\n", - "unique_travel_cities_list" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From d5d316a59a6a45ef0bc059f71f28b035f8fa3646 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:43:28 +0000 Subject: [PATCH 22/25] Automatically backed up by Learn --- .~index.ipynb | 1293 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1293 ------------------------------------------------- 2 files changed, 1293 insertions(+), 1293 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..acdafe8f6b --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1293 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Solta'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities_list = list(unique_travel_cities)\n", + "\n", + "unique_travel_cities_list[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Toronto',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Pyeongchang',\n", + " 'Los Cabos',\n", + " 'Buenos Aires',\n", + " 'Marakesh',\n", + " 'Greenville',\n", + " 'Albuquerque',\n", + " 'Salina Island',\n", + " 'Walla Walla Valley',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index acdafe8f6b..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1293 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Solta'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities_list = list(unique_travel_cities)\n", - "\n", - "unique_travel_cities_list[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 3a60a4ef62c0c74983449854c8c7944f2973a915 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:43:33 +0000 Subject: [PATCH 23/25] Automatically backed up by Learn --- .~index.ipynb | 1293 ------------------------------------------------- index.ipynb | 1291 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1291 insertions(+), 1293 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index acdafe8f6b..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1293 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Solta'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities_list = list(unique_travel_cities)\n", - "\n", - "unique_travel_cities_list[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Toronto',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Pyeongchang',\n", - " 'Los Cabos',\n", - " 'Buenos Aires',\n", - " 'Marakesh',\n", - " 'Greenville',\n", - " 'Albuquerque',\n", - " 'Salina Island',\n", - " 'Walla Walla Valley',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..f94ba22b0a 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1291 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Solta'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities_list = list(unique_travel_cities)\n", + "\n", + "unique_travel_cities_list[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Iguazu Falls',\n", + " 'Solta',\n", + " 'Archipelago Sea',\n", + " 'San Antonio',\n", + " 'Albuquerque',\n", + " 'Walla Walla Valley',\n", + " 'Greenville',\n", + " 'Toronto',\n", + " 'Marakesh',\n", + " 'Salina Island',\n", + " 'Buenos Aires',\n", + " 'Pyeongchang',\n", + " 'Los Cabos']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 14279a336941e38b087410766b05ddf9fb91a930 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:43:48 +0000 Subject: [PATCH 24/25] Automatically backed up by Learn --- .~index.ipynb | 1291 +++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 1291 ------------------------------------------------- 2 files changed, 1291 insertions(+), 1291 deletions(-) create mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb new file mode 100644 index 0000000000..f94ba22b0a --- /dev/null +++ b/.~index.ipynb @@ -0,0 +1,1291 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Solta'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities_list = list(unique_travel_cities)\n", + "\n", + "unique_travel_cities_list[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Iguazu Falls',\n", + " 'Solta',\n", + " 'Archipelago Sea',\n", + " 'San Antonio',\n", + " 'Albuquerque',\n", + " 'Walla Walla Valley',\n", + " 'Greenville',\n", + " 'Toronto',\n", + " 'Marakesh',\n", + " 'Salina Island',\n", + " 'Buenos Aires',\n", + " 'Pyeongchang',\n", + " 'Los Cabos']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/index.ipynb b/index.ipynb index f94ba22b0a..e69de29bb2 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1,1291 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Solta'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities_list = list(unique_travel_cities)\n", - "\n", - "unique_travel_cities_list[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Iguazu Falls',\n", - " 'Solta',\n", - " 'Archipelago Sea',\n", - " 'San Antonio',\n", - " 'Albuquerque',\n", - " 'Walla Walla Valley',\n", - " 'Greenville',\n", - " 'Toronto',\n", - " 'Marakesh',\n", - " 'Salina Island',\n", - " 'Buenos Aires',\n", - " 'Pyeongchang',\n", - " 'Los Cabos']" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From bc275ff2568603c1a3b2897d5ecad12528b5cda3 Mon Sep 17 00:00:00 2001 From: Christopher Collins-Larsen Date: Sat, 14 Sep 2019 07:43:53 +0000 Subject: [PATCH 25/25] Automatically backed up by Learn --- .~index.ipynb | 1291 ------------------------------------------------- index.ipynb | 1291 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1291 insertions(+), 1291 deletions(-) delete mode 100644 .~index.ipynb diff --git a/.~index.ipynb b/.~index.ipynb deleted file mode 100644 index f94ba22b0a..0000000000 --- a/.~index.ipynb +++ /dev/null @@ -1,1291 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Introduction to lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a list " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Travel Locations\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires\n", - "4. Los Cabos\n", - "5. Walla Walla Valley\n", - "6. Marakesh\n", - "7. Albuquerque\n", - "8. Archipelago Sea\n", - "9. Iguazu Falls\n", - "10. Salina Island\n", - "11. Toronto\n", - "12. Pyeongchang" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here is what that list looks like as a Python `list`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['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']" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "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": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Elements of Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", - "\n", - "1. Solta\n", - "2. Greenville\n", - "3. Buenos Aires" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "...a list in Python also assigns a number to each element." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville', 'Buenos Aires']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0] \n", - "\n", - "number_one_top_travel_cities = top_travel_cities[0]\n", - "\n", - "top_three = top_travel_cities[0:3]\n", - "\n", - "top_three\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", - "\n", - "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Buenos Aires'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Pyeongchang'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And we can move back as many as we want." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[-2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Toronto'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_canadian_city = top_travel_cities[-2]\n", - "top_canadian_city" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_canadian_city)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(top_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Accessing Multiple Elements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[0:2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", - "\n", - "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top_travel_cities' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m\u001b[0m", - "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" - ] - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a different experiment to answer our question." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities[4:5]\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Walla Walla Valley', 'Marakesh']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4:6]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta', 'Greenville']" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_two = top_travel_cities[0:2]\n", - "top_two" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Changing elements with destructive methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's take another look at `top_travel_cities`." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", - " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", - "top_travel_cities.append('San Antonio')\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities.pop()\n", - "\n", - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Walla Walla Valley'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities[4]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities[4] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 4,\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our list is changed, but now it's not as sensible, so let's change it back." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "top_travel_cities[4] = 'Walla Walla Valley'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "With that, our list is back to the way we like it." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Finding Unique elements and length of lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we are not sure whether there are repeated elements, we can use Python to get a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "top_travel_cities.append('Solta')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For example, now that we have added Solta to the end of our list, Solta appears twice." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Buenos Aires',\n", - " 'Greenville',\n", - " 'Iguazu Falls',\n", - " 'Los Cabos',\n", - " 'Marakesh',\n", - " 'Pyeongchang',\n", - " 'Salina Island',\n", - " 'San Antonio',\n", - " 'Solta',\n", - " 'Toronto',\n", - " 'Walla Walla Valley'}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities = set(top_travel_cities)\n", - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The set function initializes a new set in Python. A set is a different type collection in Python. " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(set())" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A set is just like a list, except elements do not have order and each element appears just once." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Solta'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities_list = list(unique_travel_cities)\n", - "\n", - "unique_travel_cities_list[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So let's convert this set, which has a unique list of our travel cities, into a list." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [], - "source": [ - "unique_travel_cities = list(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(unique_travel_cities)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So the array of `unique_travel_cities` is a unique list." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Iguazu Falls',\n", - " 'Solta',\n", - " 'Archipelago Sea',\n", - " 'San Antonio',\n", - " 'Albuquerque',\n", - " 'Walla Walla Valley',\n", - " 'Greenville',\n", - " 'Toronto',\n", - " 'Marakesh',\n", - " 'Salina Island',\n", - " 'Buenos Aires',\n", - " 'Pyeongchang',\n", - " 'Los Cabos']" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "unique_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And you can see quickly that it differs from the list of top travel cities by checking the length." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(unique_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(top_travel_cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Solta',\n", - " 'Greenville',\n", - " 'Buenos Aires',\n", - " 'Los Cabos',\n", - " 'Walla Walla Valley',\n", - " 'Marakesh',\n", - " 'Albuquerque',\n", - " 'Archipelago Sea',\n", - " 'Iguazu Falls',\n", - " 'Salina Island',\n", - " 'Toronto',\n", - " 'Pyeongchang',\n", - " 'San Antonio',\n", - " 'Solta']" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "top_travel_cities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Summary" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/index.ipynb b/index.ipynb index e69de29bb2..c9ddd3ffb5 100644 --- a/index.ipynb +++ b/index.ipynb @@ -0,0 +1,1291 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have worked with individual pieces of data like the string `'hello'`. Then with variables we saw how to give this data a name. Now in this lesson, we'll see how we can group data together with lists. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a list " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is our first form of a collection. A collection is just a way of grouping data together, and lists certainly accomplish this. For example, let's consider the top cities for travel according to the magazine Travel and Leisure. Here is how we are used to seeing a list of travel locations in a document or on a website." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Travel Locations\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires\n", + "4. Los Cabos\n", + "5. Walla Walla Valley\n", + "6. Marakesh\n", + "7. Albuquerque\n", + "8. Archipelago Sea\n", + "9. Iguazu Falls\n", + "10. Salina Island\n", + "11. Toronto\n", + "12. Pyeongchang" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what that list looks like as a Python `list`:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We indicate that we are initializing a `list` with an opening bracket, `[`, and we end the list with a closing bracket `]`. We separate each list item, also called an element, with a comma." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can, of course, declare variables and set them equal to our lists so that we can both name and later retrieve the list." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "countries_of_top_cities = ['Croatia', 'USA', 'Argentina', 'Mexico', 'USA', 'Morocco', 'New Mexico', 'Finland', 'Argentina', 'Italy', 'Canada', 'South Korea']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Elements of Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now our `top_travel_cities` list contains multiple elements, and just like we are used to list elements having a rank or number associated with them...\n", + "\n", + "1. Solta\n", + "2. Greenville\n", + "3. Buenos Aires" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...a list in Python also assigns a number to each element." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville', 'Buenos Aires']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0] \n", + "\n", + "number_one_top_travel_cities = top_travel_cities[0]\n", + "\n", + "top_three = top_travel_cities[0:3]\n", + "\n", + "top_three\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the above line we are referencing a list and then using the brackets to access a specific element of our list, the first element. We access elements in a list with the `index`, and there is a separate index for each element in the list. It begins at the number zero, increases for every element thereafter.\n", + "\n", + "So to access the second element we write `top_travel_cities[1]`, and the third element is `top_travel_cities[2]`." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Buenos Aires'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How would we access the last element? Well, we could count all of the elements in the list, and `Pyeongchang` would just be one less than that. Or we can ask Python to start from the end and move back one:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Pyeongchang'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we can move back as many as we want." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[-2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Each element in our list is a string, so, we can always set an element of our string equal to a variable. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Toronto'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_canadian_city = top_travel_cities[-2]\n", + "top_canadian_city" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_canadian_city)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have a variable of `top_canadian_city`, equal to the string 'Toronto', and a variable of `top_travel_cities` equal to the list of cities. " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(top_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing Multiple Elements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now imagine that we don't want to access just one element of a list, but multiple elements at once. Python allows us to do that as well:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[0:2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we can see from the above example, we can access elements of a list by placing two numbers separated by a colon inside of our brackets. The first number indicates the index of the first element we want returned. \n", + "\n", + "The second number could represent the number of elements we want returned back, or maybe it represents the stopping index of the elements that we are retrieving. Looking at our `top_travel_cities` it could be either." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'top_travel_cities' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m\u001b[0m", + "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtop_travel_cities\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'top_travel_cities' is not defined" + ] + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try a different experiment to answer our question." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities[4:5]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ok, so that second number is not representing the number of elements we want returned. Instead it must be the index at which we stop our selection of elements." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Walla Walla Valley', 'Marakesh']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4:6]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This operation is called `slice`. So, we can say we are `slicing` the elements with indices 4 and 5 in the line above. Note that even though we are `slicing` elements, our list remains intact." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In programming terms, we would say that slicing elements is non-destructive, because it does not change the underlying data structure. We can do it as many times as we like, and our `top_travel_cities` array remains unchanged. If we wish to store that slice of elements, we can store it in another variable." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta', 'Greenville']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_two = top_travel_cities[0:2]\n", + "top_two" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we have another variable called `top_two` that points to an array which contains an array of elements equal to the first two elements of `top_travel_cities`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing elements with destructive methods" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we can read and select certain elements from lists, let's work on changing these lists. To add a new element to a list, we can use the `append` method." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's take another look at `top_travel_cities`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will see that 'San Antonio' has been added to the list. Note that unlike slice, `append` is destructive. That is, it changes our underlying data structure. Every time we execute the `append` method, another element is added to our list. Now what if we accidentally add 'San Antonio' a second time to our list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities = ['Solta', 'Greenville', 'Buenos Aires', 'Los Cabos', 'Walla Walla Valley', 'Marakesh', 'Albuquerque', \n", + " 'Archipelago Sea', 'Iguazu Falls', 'Salina Island', 'Toronto', 'Pyeongchang']\n", + "top_travel_cities.append('San Antonio')\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you press shift+enter on the above line of code, we will have `'San Antonio'` as the last two elements of the list. Luckily, we have the `pop` method to remove one of them. The `pop` method is available to call on any list and removes the last element from the list. As you can see below, calling `pop` removed our last element." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities.pop()\n", + "\n", + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if we want to change an element from the middle of the list, we can access and then reassign that element. For example, let's change 'Walla Walla Valley' to the number 4." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Walla Walla Valley'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities[4] = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 4,\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our list is changed, but now it's not as sensible, so let's change it back." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "top_travel_cities[4] = 'Walla Walla Valley'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With that, our list is back to the way we like it." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Finding Unique elements and length of lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we are not sure whether there are repeated elements, we can use Python to get a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "top_travel_cities.append('Solta')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, now that we have added Solta to the end of our list, Solta appears twice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Well to see a unique list of the elements, we can call the `set` function. The set function is non-destructive on our list." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Buenos Aires',\n", + " 'Greenville',\n", + " 'Iguazu Falls',\n", + " 'Los Cabos',\n", + " 'Marakesh',\n", + " 'Pyeongchang',\n", + " 'Salina Island',\n", + " 'San Antonio',\n", + " 'Solta',\n", + " 'Toronto',\n", + " 'Walla Walla Valley'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities = set(top_travel_cities)\n", + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The set function initializes a new set in Python. A set is a different type collection in Python. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(set())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A set is just like a list, except elements do not have order and each element appears just once." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Solta'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities_list = list(unique_travel_cities)\n", + "\n", + "unique_travel_cities_list[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " So here, when we convert our list into a set, our set just consists of the unique elements. But unfortunately this structure is a set, not a list." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So let's convert this set, which has a unique list of our travel cities, into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "unique_travel_cities = list(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(unique_travel_cities)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So the array of `unique_travel_cities` is a unique list." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Iguazu Falls',\n", + " 'Solta',\n", + " 'Archipelago Sea',\n", + " 'San Antonio',\n", + " 'Albuquerque',\n", + " 'Walla Walla Valley',\n", + " 'Greenville',\n", + " 'Toronto',\n", + " 'Marakesh',\n", + " 'Salina Island',\n", + " 'Buenos Aires',\n", + " 'Pyeongchang',\n", + " 'Los Cabos']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And you can see quickly that it differs from the list of top travel cities by checking the length." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(unique_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(top_travel_cities)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Solta',\n", + " 'Greenville',\n", + " 'Buenos Aires',\n", + " 'Los Cabos',\n", + " 'Walla Walla Valley',\n", + " 'Marakesh',\n", + " 'Albuquerque',\n", + " 'Archipelago Sea',\n", + " 'Iguazu Falls',\n", + " 'Salina Island',\n", + " 'Toronto',\n", + " 'Pyeongchang',\n", + " 'San Antonio',\n", + " 'Solta']" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "top_travel_cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note:** *For most purposes, Python developers prefer to work with `lists` as opposed to sets, as `lists` are generally easier to manipulate, as you will see in future lessons.*" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to associate data together in a collection, called a list. A list is similar to a list in the real world - it implies the data has some connection, and that it has an order to it. We initialize a list with the brackets, `[]`, and separate each element by a comma. To access elements from a list, we use the bracket accessor followed by the index of the element we want to retrieve, and our indices begin at zero and increase from there. To add a new element to the end of the list we use the `append` method, and to remove an element from the end of a list we use `pop`. We can change elements anywhere between by first accessing the elements and then reassigning them." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}