Introduction to Coding in Python, Part 2

Investigative Reporters and Editors Conference, New Orleans, June 2016
By Aaron Kessler and Christopher Schnaars

Lists

A list is a mutable (meaning it can be changed), ordered collection of objects. Everything in Python is an object, so a list can contain not only strings and numbers, but also functions and even other lists.

Let's make a list of new friends we've made at the IRE conference: Aaron, Sue, Chris and Renee. We'll call our list my_friends. To create a list, put a comma-separated list of strings (our friends' names) inside square brackets ([]). These brackets are how we tell Python we're building a list. See if you can figure out what to do in the box below. If you can't figure it out, don't sweat it, and read on for the answer:


In [ ]:

Did you get it? The answer: my_friends = ['Aaron', 'Sue', 'Chris', 'Renee']

Type my_friends in the box below, and you'll see Python remembers the order of the names:


In [ ]:

We met Cora at an awesome Python class we just attended, so let's add her to our list of friends. To do that, we're going to use a method called append. A method is a bit of code associated with a Python object (in this case, our list) to provide some built-in functionality. Every time you create a list in Python, you get the functionality of the append method (and a bunch of other methods, too) for free.

To use the append method, type the name of your list, followed by a period and the name of the method, and then put the string we want to add to our list ('Cora') in parentheses. Try it:


In [ ]:


In [ ]:
my_friends

In a list, you can retrieve a single item by index. To do this, type the name of your list, followed by the numeric position of the item you want inside square brackets. There's just one sticking point: Indices in Python are zero-based, which means the first item is at position 0, the second item is at position 1 and so on. If that sounds confusing, don't worry about it. There actually are very good, logical reasons for this behavior that we won't dive into here. For now, just accept our word that you'll get used to it and see if you can figure out what to type to get the first name in our list (Aaron):


In [ ]:

You can retrieve a contiguous subset of names from your list, called a slice. To do this, type the name of your list and provide up to three parameters in square brackets, separated by colons. Just leave any parameter you don't need blank, and Python will use its default value. These parameters, in order, are:

  • The index of the first item you want. Default is 0.
  • The index of the first item you *don't* want. You can set this to a negative number to skip a specific number of items at the end of your `list`. For example, a value of -1 here would stop at the next-to-last item in your `list`. Default is the number of items in your `list` (also called the *length*).
  • The *step* value, which we can use to skip over names in our `list`. For example, if you want every other name, you could set the *step* to 2. If you want to go backwards through your `list`, set this to a negative number. Default is 1.

Use the boxes below to see if you can figure out how to retrieve these lists of names. There could be more than one way to answer each question:

  1. The first two names in the list (`['Aaron', 'Sue']`)
  2. The second and third names in the list (`['Sue', 'Chris']`)
  3. The second and fourth names in the list (`['Sue', 'Renee']`)
  4. The last three names in the list, in reverse order (`['Cora', 'Renee', 'Chris']`)

In [ ]:
# The first two names in the list (['Aaron', 'Sue'])

In [ ]:
# The second and third names in the list (['Sue', 'Chris'])

In [ ]:
# The second and fourth names in the list (['Sue', 'Renee'])

In [ ]:
# The last three names in the list, in reverse order (['Cora', 'Renee', 'Chris'])

Mutable objects

In many programming languages, it's common to assign a value (such as an integer or string) to a variable. Python works a bit differently. In our example above, Python creates a list in memory to house the names of our friends and then creates the object my_friends to point to the location in memory where this list is located. Why is that important? Well, for one thing, it means that if we make a copy of a list, Python keeps only one list in memory and just creates a second pointer. While this is not a concern in our example code, it could save a lot of computer memory for a large list containing hundreds or even thousands of objects. Consider this code:


In [ ]:
my_friends = ['Aaron', 'Sue', 'Chris', 'Renee', 'Cora']
your_friends = my_friends
print('My friends are: ')
print(my_friends)

print('\nAnd your friends are: ') # \n is code for newline.
print(your_friends)

Here's where mutability will bite you, if you're not careful. You haven't met Cora yet and don't know how nice she is, so you decide to remove her from your list of friends, at least for now. See if you can figure out what to type in the box below. You want to use the remove method to remove 'Cora' from your_friends. Use the second box below to verify Cora has been removed:


In [ ]:


In [ ]:
your_friends

Perfect! Or is it? Let's take another look at my_friends:


In [ ]:
my_friends

Uh-oh! You've unfriended Cora for me too! Remember that my_friends and your_friends are just pointers to the same list, so when you change one, you're really changing both. If you want the two lists to be independent, you must explicitly make a copy using, you guessed it, the copy method. In the box below:

  • Add Cora back to `my_friends`.
  • Use the `copy` method to assign a copy of `my_friends` to `your_friends`.
  • Remove Cora from `your_friends`.

You can use the second and third boxes below to test whether your code is correct.


In [ ]:


In [ ]:
my_friends

In [ ]:
your_friends

Dictionaries

In Python, a dictionary is a mutable, unordered collection of key-value pairs. Consider:


In [ ]:
friend = {'last_name': 'Schnaars', 'first_name': 'Christopher', 'works_for': 'USA Today', 'favorite_food': 'spam'}

Note that our data is enclosed in curly braces, which tell Python you are building a dictionary.

Now notice what happens when we ask Python to spit this information back to us:


In [ ]:
friend

Notice that Python did not return the list of key-value pairs in the same order as we entered them. Remember that dictionaries are unordered collections. This might bother you, but it shouldn't. You'll find in practice it is not a problem. Because key order varies, you can't access a value by index as you might with a list, so something like friend[0] will not work.

You might notice that the keys are listed in alphabetical order. This is not always the case. You can't assume keys will be in any other particular order.

To add a new key-value pair, simply put the new key in brackets and assign the value with an = sign. Try to add the key favorite_sketch to our dictionary, and set it's value to dead parrot:


In [ ]:


In [ ]:
friend

To replace an existing value, simply re-assign it. Change first_name to Chris:


In [ ]:


In [ ]:
friend