Sets and Dictionaries in Python: Dictionaries (Learner Version)

Objectives

  • Explain the similarities and differences between sets and dictionaries.
  • Perform common operations on dictionaries.

Lesson

  • Use a dictionary to keep track of key-value pairs
  • Its keys are like the elements of a set: unique, unordered, and immutable
  • The values associated with the keys can be anything, but can only be looked up via their keys
  • Create a dictionary:

In [1]:
birthdays = {'Newton' : 1642, 'Darwin' : 1809}
KeyValue
'Newton'1642
'Darwin'1809
  • Access values by subscripting

In [2]:
print birthdays['Newton']


1642

In [3]:
birthdays['Turing'] = 1612
print birthdays


{'Turing': 1612, 'Newton': 1642, 'Darwin': 1809}

In [4]:
birthdays['Turing'] = 1912
print birthdays


{'Turing': 1912, 'Newton': 1642, 'Darwin': 1809}

  • Can only read values that are already there

In [5]:
print birthdays['Nightingale']


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-44ca8cabb590> in <module>()
----> 1 print birthdays['Nightingale']

KeyError: 'Nightingale'
  • Test for the presence of a key using in

In [6]:
print 'Nightingale' in birthdays


False

In [7]:
print 'Darwin' in birthdays


True
  • len reports the number of items in the dictionary
  • for loops over the keys in some arbitrary order

In [8]:
print len(birthdays)
for name in birthdays:
    print name, birthdays[name]


3
Turing 1912
Newton 1642
Darwin 1809
  • Main function to count atomic symbols in a file

In [14]:
def main(filename):
    counts = count_atoms(filename)
    for atom in counts:
        print atom, counts[atom]
  • Count atoms, one per line

In [15]:
def count_atoms(filename):
    '''Count unique atoms, returning a dictionary.'''

    result = {}
    with open(filename, 'r') as reader:
	for line in reader:
	    atom = line.strip()
            if atom not in result:
                result[atom] = 1
            else:
                result[atom] = result[atom] + 1
    return result

In [16]:
main('some_atoms.txt')


Na 3
Si 1
Fe 1
Pd 1
  • Use tuples for multi-part keys

In [17]:
birthdays = {
    ('Isaac', 'Newton') : 1642,
    ('Charles', 'Robert', 'Darwin') : 1809,
    ('Alan', 'Mathison', 'Turing') : 1912
}
print birthdays


{('Charles', 'Robert', 'Darwin'): 1809, ('Isaac', 'Newton'): 1642, ('Alan', 'Mathison', 'Turing'): 1912}
  • Use the keys and values methods to get lists of keys and values

In [18]:
all_keys = birthdays.keys()
print all_keys


[('Charles', 'Robert', 'Darwin'), ('Isaac', 'Newton'), ('Alan', 'Mathison', 'Turing')]

In [20]:
all_values = birthdays.values()
print all_values


[1809, 1642, 1912]
  • Do not loop over dict.keys(), since that creates a list of keys rather than using them directly

Key Points

  • Use dictionaries to store key-value pairs with distinct keys.
  • Create dictionaries using {k1:v1, k2:v2, ...}.
  • Dictionaries are mutable, i.e., they can be updated in place.
  • Dictionary keys must be immutable, but values can be anything.
  • Use tuples to store multi-part keys in dictionaries.
  • dict[key] refers to the dictionary entry with a particular key.
  • key in dict tests whether a key is in a dictionary.
  • len(dict) returns the number of entries in a dictionary.
  • A loop over a dictionary produces each key once, in arbitrary order.
  • dict.keys() creates a list of the keys in a dictionary.
  • dict.values() creates a list of the keys in a dictionary.