Sunday, March 2, 2014

Defining Dictionary Definitions

Python has another very useful data type that I have been putting off mentioning, a dictionary. I haven’t been putting it off because it is esoteric or un-useful. On the contrary it is very useful for programming. I have been putting it off because it is a bit more complex than the more fundamental data types and it is important to understand the fundamental types before dealing with dictionaries. However, I think we are ready for them now.

Python dictionary objects are somewhat similar to real-world dictionaries. They contain, what are known as “key:value pairs”. You can think of this in terms of a real dictionary. The “key” is the word you want to look up, the “value” is the definition of the key. There are several ways to create dictionaries so I’m going to dive in and we’ll learn as we go. We’ll start by creating a blank dictionary:

d = { }

image

Curly braces generally (but not always) denote dictionaries. When creating a dictionary with entries in it the entries should be entered as key : value pairs. Generally speaking, the key is a usually a string, though it doesn’t have to be. The value can be any type of python object or function. Let’s create a dictionary that tells us about our friend bob. We’ll include his age, height, job, and hobbies.

bob = { ‘age’ : 33, ‘height’ : ‘’’6’ 0” ‘’’, ‘job’: ‘Designer’, ‘hobbies’:[‘playing guitar ’] }

image

Now I have all of the information about Bob in one python dictionary. I can access the data like so:

<dictionary name>[<key>]

In this case something like bob[‘age’]…

image

Notice that the entries are different types of objects. Dictionaries can be collections of many types of data.

image

It’s important to note that dictionaries are “unordered”. We can see this by using a built-in dictionary method called keys( ). The keys( ) method returns a list of all of the keys in our dictionary.

image

Notice that the keys are not returned in the order in which we created them. This is because the data is stored without any ordering information.

Each value object in the dictionary can be manipulated using standard methods. For instance, if we wanted to add a hobby to bob’s list of hobbies we could do so using the list’s append( ) method.

image

But what if we wanted to include additional information that wasn’t available when we created the dictionary? Not to fear, it is as simple as adding a key value pair. Let’s add bob’s birthday:

image

You can see it is almost as simple as creating a new variable, only we create the variable within the dictionary. Now I’m going to create a new dictionary for another friend.

image

Now, I’m really going to blow your mind and make a blank dictionary called friends and add bob and bill to it.

image

Now we’re really getting some complex data storage. Now if I want to access information on either friend I can do so using multiple keys:

image

Because the dictionary’s keys( ) method returns a list of keys, I can use it to loop through my friends and access information using a for loop.

for x in friends.keys():
    print x
    print “Birthday:”, friends[x][‘bday’]

image

Dictionaries have several built-in methods. I just want to mention the more common and useful ones before we wrap this up. We have already used the keys( ) method which returns a list of the dictionary’s keys. There is a similar method for returning a list of all of the values in a dictionary, creatively named values( ). It is used exactly the same way that keys( ) method is used.

Finally, there is the has_key( ) method. This method, as you might expect, tests whether a dictionary has a particular key and returns True or False. This method can be very useful because calling a dictionary with a non-existent key will result in an error. This function can be implemented to avoid those errors.

image

Though our example seems simple, dictionaries are incredibly powerful programming tools. Hopefully, this example starts to bring sites like facebook or twitter to mind. Imagine you were creating a social networking website in which each of your users input information about themselves. Then those users started linking together. You might consider using data structures like dictionaries to keep track of all that information.  Next time we will expand that power, as well as our own programming power by learning about how we can read and write files from our programs.

No comments:

Post a Comment