Overview

A Python dictionary is a set of key-value pairs where the key is the index to the value.  For example, think of a store where each item has a price:

Python
products = {
    'toothbrush': 2.50, 
    'tomato soup': 1.99
}

print(f'Tomato soup costs ${products["tomato soup"]:.2f}')

Output

Tomato soup costs $1.99

The syntax for a key-value pair is always the key, a colon, and the value.

Keys are always unique - you cannot have two entries in a dictionary with the same key.

The keys can be strings, numbers, or tuples, and the values can be any Python data type.  Furthermore, the keys and values can be mixed data types, although it is not recommended to mix the data type of the key.

Example

Python
dict = {
    1 : 'one',
    '2' : 2
}

print(dict[1])
print(dict['2'])

Output

one
2

If the key is not found in the dictionary, Python will throw a KeyError exception, except for certain functions like get().