HOWTO · Python
How to Declare a Dictionary in Python
This tutorial demonstrates how to declare a dictionary data type in Python. Learn various methods such as using curly braces, the dict() constructor, lists of tuples, and dictionary comprehension. Mastering these techniques will enhance your programming skills and enable efficient data management in your Python projects.
On this page
Dictionaries are mutable mappings from unique keys to values. Use a key to look up its value, add a new key-value pair, or replace the value associated with an existing key. This guide shows the common ways to create a dictionary and when each input form is useful.
What a Python dictionary stores
A dictionary maps each key to one value. Keys must be hashable; strings, numbers, and tuples made only of hashable values are common choices. Lists, dictionaries, and sets are unhashable, so they cannot be keys. Values can be any Python object. Assigning to an existing key replaces its value rather than creating a second entry for that key.
Create a dictionary with braces
Use braces with key: value pairs for a dictionary literal. The example creates a mapping, reads a value, adds a key, and then updates an existing key.
Run this example:
person = {"name": "Alice", "age": 30}
print(person["name"])
person["city"] = "Boston"
person["age"] = 31
print(person["age"])
print(person["city"])
Alice
31
Boston
Use {} when you need an empty dictionary.
Square brackets retrieve a value by key. Reading a key that is absent raises KeyError; use person.get("nickname") when a missing key should instead produce None (or pass a different default as the second argument).
Create a dictionary with dict()
The built-in dict() constructor can create an empty mapping with dict(). Its keyword form is convenient when the keys are valid Python identifiers: keyword names become string keys. Use braces when keys are not identifiers or when you want to make key-value pairs explicit.
person = dict(name="Bob", age=25)
print(person)
{'name': 'Bob', 'age': 25}
For example, dict(name="Bob") creates the string key "name"; it does not accept an arbitrary expression as a keyword name.
Build a dictionary from pairs or existing data
Pass an iterable of two-item iterables to dict() when data is already arranged as pairs. Each pair supplies one key and its value.
pairs = [("name", "Charlie"), ("age", 28)]
person = dict(pairs)
print(person["age"])
28
This works with other iterable pair sources too, such as tuples. If a key appears more than once in the input, the later value replaces the earlier value in the resulting dictionary. Each item must provide exactly two values.
Create a dictionary with a comprehension
A dictionary comprehension builds key-value pairs from an iterable using an expression. It is useful when each output entry follows the same transformation.
squares = {number: number ** 2 for number in range(4)}
print(squares)
{0: 0, 1: 1, 2: 4, 3: 9}
Here each number becomes a key and its square becomes the value. A comprehension can also include a condition, as in {number: number ** 2 for number in range(6) if number % 2 == 0}.
Choose a dictionary creation method
Choose the form that matches the data you have:
- Use braces for a literal whose keys and values you are writing directly, including
{}for an empty dictionary. - Use
dict()with keyword arguments when the keys are simple identifier-like names. - Use
dict(pairs)when existing data consists of two-item pairs. - Use a comprehension when values or keys are derived consistently from an iterable.
All of these create mutable dictionaries. To add or change an entry later, assign with mapping[key] = value; assigning a key that is already present replaces its value.