Digital Education Resources - Vanderbilt Libraries Digital Lab

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:

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']
a_slice = basket[1:4]
print(a_slice)

# Slicing from the beginning
print(basket[:4])

# Slicing to the end
print(basket[2:])

# Slice relative to the end
print(basket[-2:])

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

diagram of a dictionary

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 assignment

The questions for the practice assignment are in this Jupyter notebook at GitHub. It is also available as a Colab notebook.


Problem 1 solution


Problem 2 solution


Problem 3 solution


Problem 4 solution


Problem 5 solution


Problem 6 solution


Problem ex1 solution


Problem ex2 solution


Next lesson: loops


Revised 2023-10-20

Questions? Contact us

License: CC BY 4.0.
Credit: "Vanderbilt Libraries Digital Lab - www.library.vanderbilt.edu"