The Python dictionary data structure allows us to associate values with keys. A key is any immutable object. The value associated with a key can be accessed with the indexing operator. In Python, dictionaries are implemented using hash tables.
A Python dictionary is a storage method for key:value pairs. Python dictionaries are enclosed in curly brackets, {}.Dictionaries, also called associative matrices, which owe their name to collections that relate a key and a value. For example, let's look at a dictionary of protocols with names and numbers:
>>> services = {"ftp":21, "ssh":22, "smtp":25, "http":80}
The limitation with dictionaries is that we cannot create multiple values with the same key. This will overwrite the previous value of the duplicate keys. Operations on dictionaries are unique. We can combine two distinct dictionaries into one by using the update method. Also, the update method will merge existing elements if they conflict:
>>> services = {"ftp":21, "ssh":22, "smtp":25, "http":80}
>>> services2 = {"ftp":21, "ssh":22, "snmp":161, "ldap":389}
>>> services.update(services2)
>>> print services
This will return the following dictionary:
{"ftp":21, "ssh":22, "smtp":25, "http":80,"snmp":161, "ldap":389}
The first value is the key and the second is the value associated with the key. As a key, we can use any immutable value: we could use numbers, strings, booleans, or tuples, but not lists or dictionaries, since they are mutable.
The main difference between dictionaries and lists or tuples is that the values stored in a dictionary are accessed not by their index, because they have no order, but by their key, using the [] operator again.
As in lists and tuples, you can also use this operator to reassign values:
>>> services["http"]= 8080
When constructing a dictionary, each key is separated from its value by a colon, and we separate items by commas. The .keys () method will return a list of all keys of a dictionary and the .items () method will return a complete list of elements in the dictionary.
Following are examples using these methods:
- services.keys() is method that will return all the keys in dictionary.
- services.items() is method that will return the entire list of items in dictionary.
From the point of view of performance, the key within a dictionary is converted to a hash value when it is stored in order to save space and improve performance when searching or indexing the dictionary. It is also possible to print the dictionary and browse the keys in a specific order. The following code extracts the dictionary elements and then orders them:
>>> items = services.items()
>>> print items
[('ftp', 21), ('smtp',25), ('ssh', 22), ('http', 80), ('snmp', 161)]
>>> items.sort()
>>> print items
[('ftp', 21), ('http', 80), ('smtp', 25), ('snmp', 161), ('ssh', 22)]
We can extract keys and values for each element in the dictionary:
>>> keys = services.keys()
>>> print keys
['ftp', 'smtp', 'ssh', 'http', 'snmp']
>>> keys.sort()
>>> print keys
['ftp', 'http', 'smtp', 'snmp', 'ssh']
>>> values = services.values()
>>> print values
[21, 25, 22, 80, 161]
>>> values.sort()
>>> print values
[21, 22, 25, 80, 161]
>>> services.has_key('http')
True
>>> services['http']
80
Finally, you might want to iterate over a dictionary and extract and display all the "key:value" pairs:
>>> for key,value in services.items():
print key,value
ftp 21
smtp 25
ssh 22
http 80
snmp 161