Week 14: Additional Data Structures

Week 14 Goals

  • More practice using lists and iterating over lists

  • Understand how to create and use a list of lists

  • Understand how to use dictionaries

Get Week 14 In-class Code

To copy over the week 14 in-class example programs, do the following (If you have trouble with either of these steps, ask a Ninja or your professor for help):

  1. Create a w14-data-structures subdirectory in your cs21/inclass directory, and cd into it:

    $ cd ~/cs21/inclass
    $ mkdir w14-data-structures
    $ cd w14-data-structures
    $ pwd
    /home/yourusername/cs21/inclass/w14-data-structures
  2. Copy over the week 14 files into your w14-data-structures subdirectory (check that they copied successfully copied by running ls:

    $ cp ~admin21/public/w14-data-structures/* ./
    $ ls
    dictionary.py grid.py numbers.txt

Week 14 Files

  • grid.py - practice using lists of lists

  • dictionary.py - practice using dictionaries

Lists of Lists

Much of this course has focused on variables that store a single item, or a one dimensional list of data (sometimes called an "array" or a "vector"). However, the data computers process is typically multi-dimensional, e.g. image data (a 2D grid of pixel color values) or a spreadsheet of numbers.

Just as we can have a list of ints or a list of objects, we can also have a list of lists, i.e. a list in which each element is itself a list.

Then we can access: (1) the entire list; (2) one of the lists within the list; or (3) an individual element inside one of the lists within the list.

matrix = [[5, 6, 7], [3, 4, 5], [1, 2, 3], [8, 12, 11]]

# this is a list: each element is a list
print(matrix) # prints the whole list

# this is a list! each element is an int
print(matrix[0]) # prints '[5, 6, 7]'

# this is a single int
print(matrix[0][2]) # prints 7

# the last one is the same as this:
lst = matrix[0]
print(lst[2])

Dictionaries

An important concept in Computer Science is the notion of a "data structure", which describes how data are organized so that they can be accessed in an efficient manner.

A list is a data structure, but it has limitations, specifically that it only stores a collection of values but does not represent any relationship between them.

Assume we want to keep track of some people’s names and ages, e.g. "Bluey" is 7, "Bingo" is 5, etc. We could do something like this using two separate lists:

names = ['Bluey', 'Bingo', 'Bandit', 'Chilli']
ages = [7, 5, 31, 29]
print('%s's age is %d' % (names[1], ages[1]))

But using and updating these lists is error-prone, the efficiency could be improved, and there is nothing inherently connecting the names and ages lists.

A more appropriate data structure to use when we want to represent a relationship between pairs of data is a dictionary.

A dictionary is a mapping of a key to a corresponding value.

In this example, the name is the key, and it is mapped to the age, which is the value.

ages = {'Bluey': 7, 'Bingo': 5, 'Bandit': 31, 'Chilli': 29}

We can access individual values by using the key:

a = ages['Bandit']
print("Bandit's age is %d" % (a))

Note that the key is a string, so we can use a variable for it:

n = 'Bluey'
a = ages[n]
print("%s's age is %d" % (n, a))

We can also modify the value that is associated with a key:

ages['Chilli'] = 30
print("Now Chilli's age is %d" % (ages['Chilli']))

And we can also add new key/value pairs:

ages['Coco'] = 4
print("Coco's age is %d" % (ages['Coco']))

When we iterate over a dictionary, we iterate over the keys:

print("Here are the key -> value pairs")
for key in ages:
    print("%s -> %d" % (key, ages[key]))