Playing with Snakes
>
<

Chapter 8: Storing Lots of Data: Dictionaries

If debugging is the process of removing software bugs, then programming must be the process of putting them in.
-Edsger Dijkstra

So let's say we want to program a zombie simulator. You know, for the eventual uprising.

So let's make a zombie. We'll call him Braaain (it's all he can say). In order to have Braaain be in our game, we need to keep track of a few things about him, such as his speed, his ability to swim, his height, how well he can dance the song Thriller. Think about how you'd store that:

  1. speed = 5
  2. swim = false
  3. height = 70
  4. dance = "Not Bad"
  5.  
picture

But let's say we want to make another zombie. We'll call him Grawg. Now we need two sets of variables:

  1. braaain_speed = 5
  2. braaain_swim = False
  3. braaain_height = 70
  4. braaain_dance = "Not Bad"
  5.  
  6. grawg_speed = 3
  7. grawg_swim = True
  8. grawg_height = 70
  9. grawg_dance = "Excellent"
  10.  

How inefficient! And we know what happens with zombies. First one comes over for dinner and he invites a friend. Next thing you know, there's hundreds of them. There's got to be a better way. Enter dictionaries.

A normal dictionary consists of words and definitions. Back in the olden days, people would leaf through a big huge book and find the word we're looking for. Right next to it would be a definition.

picture

A Python dictionary works much the same way. We enter words, called keys, and their definitions, which we call values. Let's start by creating an empty one.

  1. braaain = {}
  2.  

Tada! We have a dictionary. Note the use of the curly brackets (some people call them a leftstache and a rightstache (due to the resemblance to a handlebar mustache, when you turn them sideways).

Now let's add some stuff to them.

  1. braaain = {}
  2. braaain['speed'] = 5
  3. braaain['swim'] = false
  4. braaain['height'] = 70
  5. braaain['dance'] = "Not bad"
  6.  

Now we've got our good friend braaain all in once place. If we want to, instead of creating someone one line at a time, we can create someone all at once.

  1. grawg = {'speed':3, 'swim':True,'height':70,'dance'="Excellent" }
  2.  

If Zombies aren't your thing, we could make a phonebook instead.

  1. phonebook = {}
  2. phonebook['Jenny'] = "867-5309"
  3. phonebook['SirMixALot'] = "1-800-mix-a-lot"
  4.  

Remember that dictionaries are of a set size. That means we can loop through them with a for loop.

  1. for name,number in phonebook.iteritems():
  2.     print name, '\t', number
  3.  

This would print out:

Jenny         867-5309
SirMixALot    1-800-max-a-lot

Notice the way we're using iteritems(). A dictionary consists of key:value pairs, so trying to loop through one directly won't necessarily work.

  1. for i in phonebook:
  2.     print i
  3.  
Jenny
SirMixALot

No phone numbers. But use iteritems() and iteritems() will take care of assigning the key to the first variable and the value to the second. We just used the variables name and number because they made sense. We could have just as easily used k and v for key and value. Remember though, you can just loop through a dictionary like normal if all you need is the key.

picture

Back to the zombies.

It's probably a bit of a pain to create a dictionary for each zombie. In fact, it'd probably be far more efficient to make a dictionary to hold our zombie dictionaries.

  1. zombies = {'braaain':{'speed':5,'swim':False,'height':70,'dance':"not bad"},'grawg':{'speed':3,'swim':True,'height':70,'dance':"Excellent"}}
  2.  

We can use our brand new dictionary of zombie dictionaries to perform operations on everyone. Here's a loop that will go through each of our zombies and increase speed by 1.

  1. for z in zombies:
  2.     zombies[z]['speed']+=1
  3.  

Check out the use of both sets of brackets. The first set references a specific zombie: z will take on the values braaain and grawg. Out of each of those dictionaries we'll look at what's stored in the value speed and increase it by 1.

Perfect! Now the zombies can eat all of us!

Must... have... braaaaaaaaaain.......

picture

This website will be taken offline before the end of 2011