Sunday, May 31, 2009

Python dictionary variables

A dictionary variable in Python is not that much different as a normal dictionary as you might have on your bookshelf. So first have a quick glimpse into the definition of a dictionary, accoording to wikipedia a dictionary is:

"A dictionary is a book or collection of words in a specific language, often listed alphabetically, with definitions, etymologies, pronunciations, and other information; or a book of words in one language with their equivalents in another, also known as a lexicon."

And this is somewhat the same as a dictionary variable we know in Python, it is a list with keys and a set of values. The keys are the index like the integer indices in a "normal" Python list variable. For example I want to make a dictionary containing all the employees per department like in the example below where you use the department name (dep0, dep1,.. in this case) as the key and the names of the people as the value. As you can see we first define the dictionary with a set of curly brackets. After that we define the key dep0 and the values attached to the key...than we take dep1.... dep2....

>>> empPerDep = {}
>>> empPerDep["dep0"]= "John","Carl","Vick"
>>> empPerDep["dep1"]= "Mark","Carl","Tom"
>>> empPerDep["dep2"]= "Tom","Bob","Jack"

Now we would like to do something with it so we can simply enter empPerDep to see what is in the dictionary however in a common situation most likely you will not want to print the entire dictionary.

>>> empPerDep
{'dep1': ('Mark', 'Carl', 'Tom'), 'dep0': ('John', 'Carl', 'Vick'), 'dep2': ('Tom', 'Bob', 'Jack')}

More likely you want to show something based upon a key like all the people in department zero. Which can be done with a statement like the one below:

>>> empPerDep["dep0"]
('John', 'Carl', 'Vick')

And in a case like this you will almost certainly want to go even more deeper and define which part of it you want to show, for example you want to show only the employee Carl, this can be done with a statement like the one below:

>>> empPerDep["dep0"][1]
'Carl'

This is all very usefull when you know what is where and you live in a very organized and structured world where everything is very predictable. However in normal world you will not be coding something is such a hardcoded way that if you want to print the name 'Carl' that you can hardcode empPerDep["dep0"][1] . You will have to make sure carl is in the dictionary and you have to find out where he is. We take a new example and use a phonebook application this time.

>>> empPhoneBook = {}
>>> empPhoneBook[12345]="Colin Aitken"
>>> empPhoneBook[12346]="Natalia A. Bochkina"
>>> empPhoneBook[12347]="Michael J Prentice"
>>> empPhoneBook[12348]="Sotirios Sabanis"
>>> empPhoneBook[12349]="Chris M Theobald"
>>> empPhoneBook[12350]="Bruce J Worton"

Now lets say we have to create a application around it, one of the things people will like to know is how many people in this university phonebook system are listed? You will be happy to see that also the len() function in Python will work on a dictionary.

>>> len(empPhoneBook)
6

now lets say you want an option where you will be able to check who has been assigned a certain number. What you can do is a empPhoneBook[76435] where 76435 is the number you want to have more details about. This is a valid option and will work as long as 76435 is a key in your dictionary. If it is not your code will generate a very nasty error.

>>> empPhoneBook[76435]
Traceback (most recent call last):
File "", line 1, in
KeyError: 76435
>>>

A better way to do this is to check before you try to retrieve. You can check if a key is in the dictionary by using the in option. This will give you a boolean back on which you can decide to try and retrieve the value.

>>> 12348 in empPhoneBook
True
>>> 76435 in empPhoneBook
False
>>>

Even do this is very useful you also want to do something like this the other way around. This is especially for a phonebook where you would like to search by name however the unique identifier is the phonenumber. So now we would like to know for example if "Colin Aitken" is in the dictionary so we do the following:

>>> "Colin Aitken" in empPhoneBook
False

Surprisingly this is giving a false, this is because it is looking into the keys not into the values. So if we want to check if the name is in the values we have to use empPhoneBook.values() instead of empPhoneBook which will only take the keys in account.

>>> "Colin Aitken" in empPhoneBook.values()
True

You will have to play around it little with dictionaries in Python to start loving them however when you do you will never give up that love to dictionaries again.

1 comment:

Paddy3118 said...

Hi Johan,

Normally we would refer to dictionary objects or types in Python as Python names can be associated with different types of objects in the lifetime of a run and strictly speaking the type is associated with the object not the name.