Containers: Dictionaries

Authors: Tom Dunham
Date: 2009-03-31

Container Types

Dicts

Dictionaries map a key to a value.

>>> di = {"a" : "man", "a plan" : "a canal", "panama": [] }
>>> di
{'a': 'man', 'panama': [], 'a plan': 'a canal'}

Dicts

You get to the values by providing the keys

>>> di
{'a': 'man', 'panama': [], 'a plan': 'a canal'}
>>> di["a plan"]
'a canal'
>>> di["panama"]
[]

Dicts

It's an error to attempt to get a key that does not exist

>>> di
{'a': 'man', 'panama': [], 'a plan': 'a canal'}
>>> di["man"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'man'

Member Tests

>>> di
{'a': 'man', 'panama': [], 'a plan': 'a canal'}
>>> di.has_key("man")
False
>>> "man" in di
False
>>> "a" in di
True

Dicts

Dictionaries are mutable

>>> di
{'a': 'man', 'panama': [], 'a plan': 'a canal'}
>>> di["a"] = 1
>>> di
{'a': 1, 'panama': [], 'a plan': 'a canal'}

Adding values

Add a new key/value pair by assigning to a new key

>>> di
{'a': 1, 'panama': [], 'a plan': 'a canal'}
>>> di[21] = "years"
>>> di
{'a': 1, 'panama': [], 'a plan': 'a canal', 21: 'years'}

Keys can be objects of any immutable type (strings, numbers, some more...)

Updating

Assign a new value to an existing key

>>> di
{'a': 1, 'panama': [], 'a plan': 'a canal', 21: 'years'}
>>> di["a plan"] = "tenet"
>>> di
{'a': 1, 'panama': [], 'a plan': 'tenet', 21: 'years'}

Deleting

>>> di
{'a': 1, 'panama': [], 'a plan': 'tenet', 21: 'years'}
>>> del di[21]
>>> di
{'a': 1, 'panama': [], 'a plan': 'tenet'}

Or clear to delete everything.

>>> di.clear()
>>> di
{}

Methods

To get a list of keys

>>> di
{'a': 'man', 'panama': [], 'a plan': 'a canal'}
>>> di.keys()
['a', 'panama', 'a plan']

To get a list of values

>>> di.values()
['man', [], 'a canal']

Methods

To get the key/value pairs, use items

>>> di.items()
[('a', 'man'), ('panama', []), ('a plan', 'a canal')]

Those () are tuples, more about them soon

Exercise

See handout

Using these two lists

ages = [22, 21, 21, 19, 19, 22, 21, 22, 20, 20, 20, 21, 20, 22, 23]
names =  ['Gladdie', 'Waltner', 'Ankeny', 'Fasto', 'Arondel', 'Virginia', 'Benetta', 'Fi',
          'Desmund', 'Blancha', 'Hackney', 'Kaleena', 'Mont', 'Elurd', 'Becker']
  1. Create a dictionary mapping a person's name to their age

  2. Write a function that takes an age as a parameter and returns a list of people who are that age.

    Test it using the following (each line should print True):

    print people_aged(19) == ['Fasto', 'Arondel']
    print people_aged(20) == ['Desmund', 'Blancha', 'Hackney', 'Mont']
    print people_aged(21) == ['Waltner', 'Ankeny', 'Benetta', 'Kaleena']
    print people_aged(22) == ['Gladdie', 'Virginia', 'Fi', 'Elurd']
    print people_aged(23) == ['Becker']
    
  3. Given a list mapping condon to amino acid

    ['TCA S Serine',
     'TCC S Serine',
     'TCG S Serine',
     'TCT S Serine',
     'TTC F Phenylalanine',
     'TTT F Phenylalanine',
     'TTA L Leucine',
     'TTG L Leucine',
     'TAC Y Tyrosine',
     'TAT Y Tyrosine',
     'TAA _ Stop',
     'TAG _ Stop',
     'TGC C Cysteine',
     'TGT C Cysteine',
     'TGA _ Stop',
     'TGG W Tryptophan',
     'CTA L Leucine',
     'CTC L Leucine',
     'CTG L Leucine',
     'CTT L Leucine',
     'CCA P Proline',
     'CCC P Proline',
     'CCG P Proline',
     'CCT P Proline',
     'CAC H Histidine',
     'CAT H Histidine',
     'CAA Q Glutamine',
     'CAG Q Glutamine',
     'CGA R Arginine',
     'CGC R Arginine',
     'CGG R Arginine',
     'CGT R Arginine',
     'ATA I Isoleucine',
     'ATC I Isoleucine',
     'ATT I Isoleucine',
     'ATG M Methionine',
     'ACA T Threonine',
     'ACC T Threonine',
     'ACG T Threonine',
     'ACT T Threonine',
     'AAC N Asparagine',
     'AAT N Asparagine',
     'AAA K Lysine',
     'AAG K Lysine',
     'AGC S Serine',
     'AGT S Serine',
     'AGA R Arginine',
     'AGG R Arginine',
     'GTA V Valine',
     'GTC V Valine',
     'GTG V Valine',
     'GTT V Valine',
     'GCA A Alanine',
     'GCC A Alanine',
     'GCG A Alanine',
     'GCT A Alanine',
     'GAC D Aspartic Acid',
     'GAT D Aspartic Acid',
     'GAA E Glutamic Acid',
     'GAG E Glutamic Acid',
     'GGA G Glycine',
     'GGC G Glycine',
     'GGG G Glycine',
     'GGT G Glycine']
    

Translate the following DNA into a protein

CGACGTCTTCGTACGGGACTAGCTCGTGTCGGTCGC

And check your result is RRLRTGLARVGR. You may find it helpful to read the help on the range function when you are considering how to approach this.

4. optional There is another way to approach question 2. Read the help on defaultdict in the collections module, and the built-in list function. In particular, note that list() == [].