Module dictionaries
source code
Dictionaries store mappings between many kinds of data types.
First and most commonly, strings can be the 'keys' of a
dictionary:
>>> X = {'x':42, 'y': 3.14, 'z':7}
>>> Y = {1:2, 3:4}
>>> Z = {}
Make some assignments into a dict
>>> L = dict(x=42,y=3.14,z=7)
The following is the way to do the same with integer keys.
>>> M = dict([[1, 2],[3, 4]])
Retrieving values:
>>> X['x']
42
>>> X['y']
3.1400000000000001
>>> M[1]
2
The following raises a KeyError:
>>> X['w']
...
KeyError: 'w'
Dictionaries and lists can be interleaved. Lists can contain
dictionaries and vice versa:
>>> Double = [{'x':42,'y': 3.14, 'z':7},{1:2,3:4},{'w':14}]
Any expression which has a Dictionary as its value may be followed by
'[key]'. The entire expresion then has the value of that key
as its value.
>>> Double[0]
{'y': 3.1400000000000001, 'x': 42, 'z': 7}
>>> Double[0]['x']
42