Python Dictionary

Python Dictionary (or dict) is a type of data. Just like a string, int, float, list, etc. You can have a datatype called dictionary. It’s key characteristic is the well known key:value pair layout.

Think of these as mini vlookups in Excel. The key refers to the item that you want to find, the value refers to the data related to the key. These are extremely helpful when you want to have a map that will return you information.

The keys in a dictionary must be unique. Values do not have to be unique. Keys and values are separated by a colon : .

my_dict = {
    'key1' : 'value1',
     key2  :  value2,
    'key3' : 'value3',
}

A dictionary is a type of mapper in python. That means you put something in, and something comes out.

You can get creative with the data types you have as keys and values. Python can handle ints, strings and floats. Dictionary keys can not be lists.

Let’s take a look at a python dictionary code sample


Python Dictionary

Dictionaries are a data type within Python. You can hold keys and their corresponding values. Think of them as small (or large) look up tables.

In [1]:
my_dict = {
    "San Francisco" : "California",
    "New York" : "New York",
    1 : 3,
    32.2 : 'Float'
}

You can access the values in a dictionary by calling on of the keys within square brackets []

In [2]:
my_dict["San Francisco"]
Out[2]:
'California'
In [3]:
my_dict["New York"]
Out[3]:
'New York'
In [4]:
my_dict[1]
Out[4]:
3
In [5]:
my_dict[32.2]
Out[5]:
'Float'

Link to code above

Check out more Python Vocabulary on our Glossary Page