Python Dictionaries

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:

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

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


Tomato soup costs $1.99

Here, 'products' is a dictionary. It contains a set of key/value pairs, name 'toohbrush' as the key and 2.50 as the value, and another pair with 'tomato soup' as the key, and 1.39 as the value

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.


dict = {
    1   : 'first item',
    '2' : 1000



first item

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