Search⌘ K

Dictionaries

Explore Python dictionaries to understand storing data as key-value pairs, creating dictionaries with different data types, and accessing values safely using keys or the get method. This lesson helps you manage data in an unordered structure effectively and avoid common errors.

Structure

A dictionary has a slightly more complex structure than a list or tuple. When we think of a dictionary, we imagine a vast book containing words and their meanings. In simpler terms, information is stored in pairs of words and meanings. Python’s dictionary data structure follows the same structure. A dictionary stores key-value pairs, where each unique key is an index that holds the value associated with it. Dictionaries are unordered because the entries are not stored in a linear structure. In Python, we must put the dictionary’s content inside curly brackets, {}:

A key-value pair is written in the following format:

key:value

Creating a dictionary

Creating a dictionary in Python is straightforward. Here's how to create an empty dictionary and a simple phone_book using the dictionary data ...