Python Wiki
Advertisement

General Information[]

Dictionaries is a data type in Python.
This type is closely related to Lists, but it has the key advantage of so-called Keys. That also explains the name, we have a key and the description to it. Furthermore, this can contain several types, but the key has to be a String.
To explain this, we look at the Syntax:

The Syntax[]

Lets look at a basic example of a Dictionary:

>>> my_dict = { 'Hello' : 'World!', 'other' : 'do not know' }
>>> my_dict['Hello']
'World!'
>>> my_dict['other']
'do not know'

Please note the structure:

{ keyword : description, ... }

The main advantage here is that we don't have to write something abstract like my_dict[0] as in lists.

Operations on dictionaries[]

Python let us operate in different ways on dictionaries:

Adding elements[]

To add a new element, we just call it:

>>> my_dict['forname'] = 'surname'
>>> my_dict
{'Hello' : 'World!', 'other' : 'do not know', 'forname' : 'surname'}

Changing values[]

To change values/descriptions of already existing entries, we call it again:

>>> my_dict['Hello'] = 'Universe'
>>> my_dict
{'Hello' : 'Universe!', 'other' : 'do not know', 'forname' : 'surname'}

Deleting entries[]

To delete entries from this structure, we use the widely used del command:

>>> del my_dict['other']
>>> my_dict
{'Hello' : 'World!', 'forname' : 'surname'}

Built-in functions and attributes[]

keys()[]

This function list up all keys:

>>> my_dict.keys()
['Hello','other','forname']

get(KEY)[]

This function returns the value of the key KEY in the dictionary:

>>> my_dict.get('Hello')
'World!'

has_key(KEY)[]

This function returns True, if the key KEY is an element of the dictionary, False if not:

>>> my_dict.has_key('Hello')
True
>>> my_dict.has_key('Helle')
False

items()[]

This function zips up all elements of a dictionary to a list of tuples, where the left side of the tuple is the key and the right one the value:

>>> my_dict.items()
[('Hello', 'World!'), ('other', 'do not know'), ('forname', 'surname') ]

pop(KEY)[]

This well-known function returns the entry with the key KEY from the dictionary and returns this element:

>>> my_dict.pop('other')
'do not know'
>>> my_dict
{'Hello' : 'World!', 'forname' : 'surname'}

values()[]

This function is the exact opposite of the keys function, so it returns a list of all values:

>>> my_dict.values()
['World!', 'do not know', 'surname']

History[]

Major Minor Details
0 0.9 Added the type “dictionary”.

The key of a dictionary could only be a string.

Only empty dictionaries could be created at the initialization by {}.

1 1.3 Now dictionaries could be created by { keyword : description, ... }.

Now any object could be the key of a dictionary.

2 2.0 Added a method setdefault to dictionaries.
2.1 Added a method popitem to dictionaries.
2.2 Added 3 methods iteritems, iterkeys, itervalues to dictionaries.

Renamed the type name from “dictionary” to “dict”.

2.3 Added 2 methods fromkeys, pop to dictionaries.
2.7 Added 3 methods viewitems, viewkeys, viewvalues to dictionaries.
3 Removed 6 methods iteritems, iterkeys, itervalues, viewitems, viewkeys, viewvalues from dictionaries.
Advertisement