Dictionaries#

A dictionary is a mapping from a key to a value. It is kind of like a list of pairs.
You can create a dictionary using curly braces.
As with lists, commas separate elements in dictionaries.

starbucks_dict = {'price': 83, 'name': 'Starbucks Corp', 'ticker': 'SBUX', 'public': True}
starbucks_dict
{'price': 83, 'name': 'Starbucks Corp', 'ticker': 'SBUX', 'public': True}

You can also place elements on their own lines to make it more readable. This does exactly the same thing. Just be careful with your commas.
Notice there is not a comma after 'public': True because it is the last entry in the dictionary.

starbucks_dict = {
    'price': 83,
    'name': 'Starbucks Corp',
    'ticker': 'SBUX',
    'public': True
}

Access individual values from the dictionary using square brackets that contain the key:

print(starbucks_dict['price'])
print(starbucks_dict['public'])
print(starbucks_dict['ticker'])
print(starbucks_dict['name'])
83
True
SBUX
Starbucks Corp

You can also add a new mapping, or pair, to the dictionary.
Or even change the value of an existing element.

starbucks_dict['ceo'] = 'Johnson'
starbucks_dict['price'] = 85

starbucks_dict
{'price': 85,
 'name': 'Starbucks Corp',
 'ticker': 'SBUX',
 'public': True,
 'ceo': 'Johnson'}