Learning Resources
Dictionary
A dictionary is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details). Note that the key must be unique just like you cannot find out the correct information if you have two persons with the exact same name.
Note that you can use only immutable objects (like strings) for the keys of a dictionary but you can use either immutable or mutable objects for the values of the dictionary. This basically translates to say that you should use only simple objects for keys.
Pairs of keys and values are specified in a dictionary by using the notation d = {key1 : value1, key2 : value2 }. Notice that the key-value pairs are separated by a colon and the pairs are separated themselves by commas and all this is enclosed in a pair of curly braces.
Remember that key-value pairs in a dictionary are not ordered in any manner. If you want a particular order, then you will have to sort them yourself before using it.
The dictionaries that you will be using are instances/objects of the dict class.
Example:
#!/usr/bin/python # Filename: using_dict.py # 'ab' is short for 'a'ddress'b'ook ab = { 'Swaroop' : '[email protected]', 'Larry' : '[email protected]', 'Matsumoto' : '[email protected]', 'Spammer' : '[email protected]' } print("Swaroop's address is", ab['Swaroop']) # Deleting a key-value pair del ab['Spammer'] print('\nThere are {0} contacts in the address-book\n'.format(len(ab))) for name, address in ab.items(): print('Contact {0} at {1}'.format(name, address)) # Adding a key-value pair ab['Guido'] = '[email protected]' if 'Guido' in ab: print("\nGuido's address is", ab['Guido'])
Output:
$ python using_dict.py Swaroop's address is [email protected] There are 3 contacts in the address-book Contact Swaroop at [email protected] Contact Matsumoto at [email protected] Contact Larry at [email protected] Guido's address is [email protected]
How It Works:
We create the dictionary ab using the notation already discussed. We then access key-value pairs by specifying the key using the indexing operator as discussed in the context of lists and tuples. Observe the simple syntax.
We can delete key-value pairs using our old friend - the del statement. We simply specify the dictionary and the indexing operator for the key to be removed and pass it to the del statement. There is no need to know the value corresponding to the key for this operation.
Next, we access each key-value pair of the dictionary using the items method of the dictionary which returns a list of tuples where each tuple contains a pair of items - the key followed by the value. We retrieve this pair and assign it to the variables name and address correspondingly for each pair using the for..in loop and then print these values in the for-block.
We can add new key-value pairs by simply using the indexing operator to access a key and assign that value, as we have done for Guido in the above case.
We can check if a key-value pair exists using the in operator.
For the list of methods of the dict class, see help(dict).
- Keyword Arguments and Dictionaries - On a different note, if you have used keyword arguments in your functions, you have already used dictionaries! Just think about it - the key-value pair is specified by you in the parameter list of the function definition and when you access variables within your function, it is just a key access of a dictionary (which is called the symbol table in compiler design terminology).