Digital Scholarship Resources - Vanderbilt Libraries Digital Scholarship and Communications Office

Previous lesson: Using code libraries

# Lists and dictionaries

In this lesson we introduce two complex objects: lists and dictionaries. A list is a named one-dimensional data structure that allows us to store any number of items and reference them by an index. A dictionary is also a one-dimensional data structure, but its elements are referenced by name using a key. The lesson describes several ways lists are created. It also introduces some ways to manipulate and edit lists. The lesson concludes by describing how to create and edit dictionaries.

Learning objectives At the end of this lesson, the learner will:

• describe a list object.
• construct a list by specifying its members.
• refer to a list item by its index.
• create lists using functions and methods.
• extract part of a list by slicing.
• randomize or order (alphabetize) a list.
• randomly select an item from a list.
• modify, remove, or add a list item.
• create a dictionary by specifying the items it contains.
• add or change dictionary values.
• remove a dictionary value.

Total video time: (32m 05s)

Lesson Jupyter notebook at GitHub

Lesson Colab notebook

Lesson slides

# Lists

## Introduction to lists (5m48s)

A list is a sequence of objects. The objects may be the same or different, but often are the same. The order of the list is important and items can be referenced by their position in the list, numbered from zero.

A list is created by putting the sequence in square brackets, separated by commas. In the following example, a list is assigned to a variable:

``````basket = ['apple', 'orange', 'banana', 'lemon', 'lime']
``````

To reference a particular item, write the variable name followed by square brackets containing the index (position) of the object in the sequence: `basket[2]`.

To determine the count of items in a list, use the `len()` function. In this example, it would be `len(basket)`, which would have a value of 5.

## Lists as output of functions (5m44s)

`listdir()` (list directory) function from `os` module.

``````from os import listdir

items_in_working_directory = listdir() # no argument for current working directory
print(items_in_working_directory)
``````

`sample()` function from random module

`random` module documentation

``````import random

population = int(input('How many items in hat? '))
n = int(input('How many items to draw from hat? '))
# sample without replacement
pull_from_hat = random.sample(range(population), n) # first argument is a range object, second is number of samples
print(pull_from_hat)
``````

`.split()` string method

``````my_sentence = 'It was a dark and stormy night.'
words_list = my_sentence.split(' ')
print(words_list)
``````

## Slicing a list (5m07s)

A slice of the list can be referenced using the following notation: `basket[1:4]`. Important note: in Python, when ranges are specified, for some reason, the last number in the range is one greater than the actual position in the range. So in this example, items 1 through 3 will be included. Since counting in Python is zero based, that means that the slice will contain the second through fourth items.

``````basket = ['apple', 'orange', 'banana', 'lemon', 'lime']
print(a_slice)

# Slicing from the beginning

# Slicing to the end

# Slice relative to the end
``````

Retrieving parts of strings uses the same notation as lists. (You can essentially think of a string as a list of characters.) So to get a particular character:

``````nobelPeacePrize = 'Dag Hammarskj\u00f6ld'
print(nobelPeacePrize[2])
``````

and to get part of a string, use a slice:

``````nobelPeacePrize = 'Dag Hammarskj\u00f6ld'
print(len(nobelPeacePrize))
print(nobelPeacePrize[12:15])
``````

Notice that escaped characters count as a single character even if we write them as an escape sequence using several characters.

## Manipulating lists (4m15s)

Randomize a list. Use the `shuffle()` function from the `random` module.

Pass in the list to be randomized as the argument.

There is no return value.

``````import random as r

cards = ['Ac', '2c', 'Jc', 'Qc', 'Kc', 'Ah', '2h', '3h']
# Shuffle acts on the list object. It does not return a list.
r.shuffle(cards)
print(cards)
``````

Sort a list. Use the `.sort()` list method.

No argument is required, although the argument `reverse=True` can be passed to sort descending (or reverse alphabetical order).

There is no return value

``````day_list = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
# Sort acts on the list item. It does not return a list.
day_list.sort()
print(day_list)
day_list.sort(reverse=True)
print(day_list)
``````

Pick a random item from a list. Use the `choice()` function from the `random` module.

The list to pick from is passed in as an argument.

The return value is the selected list item.

``````day_list = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
# Choice returns a single item from a list
print(day_list)
picked_day = r.choice(day_list)
print(picked_day)
``````

## Modifying a list (4m20s)

To add an item to a list, use the `.append()` method. Here is an example:

``````basket.append('durian')
``````

Notice that there is no assignment with this method – you simply apply it and the list itself is changed.

A list can also be empty. You can create an empty list like this:

``````hungry = []
``````

You can then add items to the list using the `.append()` method.

To change an item in a list, just assign a new value to that item:

``````basket[1] = 'tangerine'
``````

To find the position of an item in the list, use the `.index()` method. You can combine this with the previously described statement to replace an item. This is useful if you don’t know the position of an item in the list.

``````basket[basket.index('banana')] = 'plantain'
``````

To remove an item from the list using its value, use the `.remove()` method:

``````basket.remove('banana')
``````

You can also delete an item using its index number:

``````del basket[3]
``````

To insert an item at a particular position in the list, use the `.insert()` method. The first argument is the position and the second is the item to be inserted.

``````basket.insert(1, 'applesauce')
``````

Two lists can be combined using the `+` operator.

# Dictionaries

## Introduction to dictionaries (6m51s)

Dictionaries are written as a series of key:value pairs, separated by commas, within curly brackets.

``````catalog = {'1008':'widget', '2149':'flange', '19x5':'smoke shifter', '992':'poiuyt'}
``````

An empty dictionary can be created using curly brackets with nothing inside them

``````traits = {}
``````

Both creating and changing a value in the dictionary are done by assigning a value by designated key

``````traits['height'] = 12
``````

An item can be removed using the del command

``````del traits['eye color']
``````

# Practice exercise

1. Create a list containing the names of the days of the week. Print the list item for Tuesday.
2. Assign a URL (including the `http://` or `https://` part) to a variable, then use the `.split()` method to break the URL into pieces based on the position of `/` characters. Note: select and copy a URL from the URL bar of a browser to get the full URL. Which list item contains the domain name (e.g. `vanderbilt.edu` or `calendar.google.com`)? Will that always be the same? Write an expression for the domain name, and print it.
3. If you have a domain name like `vanderbilt.edu`, `www.library.vanderbilt.edu`, or `calendar.google.com`, how can you determine how many parts it has that are separated by periods (dots)? (Hint: first split it into a list.) How can you write a general expression for the last part of any domain name? Modify your script from the last exercise to allow a user to paste in a full URL copied from a browser bar, then tell the user the kind of domain (`com`, `edu`, `org`, etc.) for the URL. Note: copy the URL from the browser bar so that it will have the `http://` or `https://` part of the URL.
4. Write a script that instantiates the fruit basket list, allows a user to type in a fruit, then add the fruit they typed in to the end of the list.
5. Modify the previous script to pick a random fruit from the modified list and print it.
6. Modify the previous script to pick a random fruit from the list, remove it from the list, tell the user that they can’t have that fruit, then print the new list without the fruit you removed.
7. Let the user type their full name with spaces between the name parts. Split the name into parts. For the first and last name, extract the first letter of each, convert them to lower case, and concatenate those initials into a single string. Print the resulting string and tell the user that it is their initials.
8. Create a dictionary where the keys are the names of months and the values are the number of days in that month. Allow a user to enter the name of a month and print the number of days in that month. Create appropriate prompts and print statements so the user knows what to do and can interpret the response.
9. Copy the statement that creates the dictionary that is a catolog of items. Create another dictionary with exactly the same items and keys, but for which the values are prices as decimal numbers. Let the user type in a catalog number, then print the name of the item and its price. Prepend a dollar sign `\$` to the price before printing, but don’t include the dollar sign in the dictionary.
10. Modify the last program to convert the price of the selected item to euros (you may need to look up the exchange rate). Add an additional statement to print the price in euros, prepending the Unicode string for the euro sign (`\u20AC`) to the numeric price. Can you make the print statement work so that there is no space between the euro sign and the price?

Next lesson: loops

Revised 2022-01-09