Extras: Dictionaries and Tuples

posted by: Ms. Martin 1 June 2010 No Comment

I hope several of you will attempt the Star Map program.  It’s challenging, interesting and hopefully really rewarding so I strongly encourage you to attempt it!

Dictionaries

Dictionaries are used to map values called keys to data or values.   In a “real world” dictionary,  the words are keys and the values are the definitions.  Dictionaries are a lot like lists but can use arbitrary values as indexes rather than being limited to integers.  Here’s how you could visualize a dictionary that maps people’s names to their favorite foods:

----------------------------------------
|"Anna"    |  "Joe"  | "Lisa" | "Bart" |
----------------------------------------
|"Pasta"   | "Candy" | "Nuts" | "Pears"|
----------------------------------------

This is very similar to how we would visualize a list, but the indexes are “Anna”, “Joe”, “Lisa” and “Bart” instead of being 0, 1, 2, 3.

Here’s a shorthand way of building this dictionary:

>>> foods = {"Anna":"Pasta", "Joe":"Candy", "Lisa":"Nuts", "Bart":"Pears"}
>>> type(foods)
>>> <type 'dict'>

Try it at the console.  You need a series of key-value pairs separated by commas.  You can use any type for key and value.  For example, you could map a name to a list of foods they like or names to phone numbers.

In order to find out what Anna likes, you can use the following command:

>>> foods["Anna"]

See how that’s just like indexing into a list, but using a str index instead of an int?

What if you want to print what each person likes without the name?  You can loop over all the keys and use them to index into the dictionary as follows:

>>> for name in foods: # for each person in the dictionary
>>>     print foods[name] # prints a favorite food without the name

You can check to see if some value is a key in a given dictionary using the in keyword:

>>> if "Helene" in foods:
>>>     print "The teacher's favorite food is: ", foods["Helene"]
>>> else:
>>>     print "No clue what the teacher likes"
No clue what the teacher likes

Exercises

1. Write a function that reads the words in a file and stores them as keys in a dictionary. It doesn’t matter what the values are. Then you can use the in operator as a fast way to check whether a string is in the dictionary.

2. Write a function named get_letter_inventory which, given a string, returns a dictionary of letter frequencies.

Here’s how it works:

>>> h = get_letter_inventory('brontosaurus')
>>> print h
{'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1}

Tuples

Tuples are also related to lists but like strings, they are immutable (cannot be changed).  Tuples are commonly surrounded in parentheses when created (this isn’t strictly necessary, but it makes things clearer).

>>> coords = (500, 200)
>>> type(coords)
<type 'tuple'>

Elements of tuples are accessed just like list elements using their 0-based index values:

>>> coords[0]
500
>>> coords[1]
200
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 2.33 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>