Dictionary (Data Structures)
Data structures are basically just that - they are structures which can hold some data together. In other words, they are used to store a collection of related data. In this module we will be working with dictionaries.
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:
# 'ab' is short for 'a'ddress'b'ook
= {
ab 'Swaroop': 'swaroop@swaroopch.com',
'Larry': 'larry@wall.org',
'Matsumoto': 'matz@ruby-lang.org',
'Spammer': 'spammer@hotmail.com'
}
print("Swaroop's address is", ab['Swaroop'])
# Deleting a key-value pair
del ab['Spammer']
print('There are', len(ab), 'contacts in the address-book')
# Adding a key-value pair
'Guido'] = 'guido@python.org'
ab[
if 'Guido' in ab:
print("\nGuido's address is", ab['Guido'])
Output:
Swaroop's address is swaroop@swaroopch.com
There are 3 contacts in the address-book
Guido's address is guido@python.org
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.
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
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).