Skip to main content

Dictionaries

Dictionaries are way to store data in linked pairs.

This is the structure of a dictionary:

{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}

Unlike lists, they are wrapped by curly braces {}. Each key has a corresponding value that is linked to it.

A real life example would be a dictionary of people and their birthdays.

{
"John": "January 1st",
"Jane": "January 2nd",
"Joe": "January 3rd",
}

Another example are the prices of various stocks.

{
"AAPL": 150.17,
"GOOGL": 2235.55,
"MSFT": 256.72,
}

To access the values of a dictionary, give the dictionary a name by assigning the dictionary by a variable.

stock_prices = {
"AAPL": 150.17,
"GOOGL": 2235.55,
"MSFT": 256.72,
}

Then, similar to lists, instead of using the index, you can use the key to access the value.

stock_prices["AAPL"] # 150.17