Computer Science · Ch 10 — Tuples and Dictionaries
Introduction to Dictionaries
10.7
Introduction to Dictionaries
The dictionary data type belongs to the mapping category of Python types. A dictionary is a mapping between a set of keys and a set of values — instead of numbering its contents by position the way a tuple or list does, it labels each piece of data with a key.
The essential vocabulary:
- A key-value pair is called an item.
- Within an item, the key is separated from its value by a colon (
:). - Consecutive items are separated by commas.
So a dictionary looks like a collection of key: value entries, one per item:
>>> capitals = {'India': 'New Delhi', 'Japan': 'Tokyo', 'France': 'Paris'}
Here 'India': 'New Delhi' is one item; 'India' is the key and 'New Delhi' is its value. …