dictionary

dictionary class

dictionary — A set of key: value pairs, with the requirement that the keys are unique.

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'jack': 4098, 'guido': 4127, 'irv': 4127}
>>> list(tel)
['jack', 'guido', 'irv']
>>> sorted(tel)
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False

Are sometimes found in other languages as “associative memories” or “associative arrays”.

Indexed by keys, which can be any immutable type.

dict Comprehension

>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}

Used to create dictionaries from arbitrary key and value expressions.

dictionary — Structure map

Clickable & Draggable!

dictionary — Related pages: