Dictionaries in Python

This lesson covers similar material to the video lessons you just completed.

This text is part of a Real Python tutorial by Leodanis Pozo Ramos.


Python dictionaries are a powerful built-in data type that allows you to store key-value pairs for efficient data retrieval and manipulation. Learning about them is essential for developers who want to process data efficiently. In this lesson, you’ll explore how to create dictionaries using literals as well as how to use Python’s operators and built-in functions to manipulate them.

Creating Dictionaries in Python

You can create Python dictionaries in a couple of ways, depending on your needs. The most common way is to use dictionary literals, which are a comma-separated series of key-value pairs in curly braces. The second way is to use the dict() constructor, which lets you create dictionaries from iterables of key-value pairs, other mappings, or a series of keyword arguments. It also lets you create empty dictionaries when you call it without arguments.

In the following sections, you’ll dive deeper into how to create Python dictionaries using literals.

Dictionary Literals

You can define a dictionary by enclosing a comma-separated series of key-value pairs in curly braces ({}). To separate the keys from their values, you need to use a colon (:). Here’s the syntax for a dictionary literal:

Language: Python Syntax
{
    <key_1>: <value_1>,
    <key_2>: <value_2>,
      ...,
    <key_N>: <value_N>,
}

The keys and values are completely optional, which means that you can use an empty pair of curly braces to create an empty dictionary. Then, you have the keys, a colon, and the value associated with the current key. To separate the pairs, you use a comma.

The keys must be hashable objects like numbers, strings, or tuples. You don’t need to dive into the details of hashable objects at this stage. Here’s all you need to know for now: In Python, the built-in immutable data types are hashable, and the mutable types are unhashable.

The following code defines a dictionary that maps cities or states to the names of their corresponding Major League Baseball (MLB) teams:

Language: Python
>>> MLB_teams = {
...     "Colorado": "Rockies",
...     "Chicago": "White Sox",
...     "Boston": "Red Sox",
...     "Minnesota": "Twins",
...     "Milwaukee": "Brewers",
...     "Seattle": "Mariners",
... }

You can only use hashable Python objects as dictionary keys. The following example shows a dictionary with integer, float, and Boolean objects used as keys:

Language: Python
>>> {42: "aaa", 2.78: "bbb", True: "ccc"}
{42: 'aaa', 2.78: 'bbb', True: 'ccc'}

You can even use objects like data types and functions as keys:

Language: Python
>>> types = {int: 1, float: 2, bool: 3}
>>> types
{<class 'int'>: 1, <class 'float'>: 2, <class 'bool'>: 3}

>>> types[float]
2
>>> types[bool]
3

However, you can’t use unhashable objects as keys. If you try to, then you’ll get an error:

Language: Python
>>> {[1, 2]: "A list as a key? Hmm..."}
Traceback (most recent call last):
    ...
TypeError: unhashable type: 'list'

Python lists are unhashable. In practice, you can’t use any mutable data type as a key in a dictionary. This means that lists, sets, and dictionaries themselves aren’t allowed.

If you need to use sequences as dictionary keys, then you can use tuples because tuples are immutable:

Language: Python
>>> a_dict = {(1, 1): "a", (1, 2): "b", (2, 1): "c", (2, 2): "d"}

>>> a_dict[(1, 1)]
'a'
>>> a_dict[(2, 1)]
'c'

It’s important to note that even though tuples are immutable, they can contain mutable objects. You can’t use a tuple that contains mutable objects as a dictionary key:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

You must own this product to join the conversation.