Last modified: Jan 27, 2026 By Alexander Williams

Python Dict Literal Guide & Examples

A dictionary literal is a direct way to create a dictionary in Python. It uses curly braces {} and key-value pairs. This method is clear and efficient. It is the most common way to define dictionaries in code.

What is a Dictionary Literal?

In Python, a dictionary is a collection of key-value pairs. A literal is a notation for representing a fixed value in source code. Therefore, a dictionary literal is the syntax used to write a dictionary directly into your program.

It is also known as "dict display" in the Python documentation. The syntax is intuitive. You enclose comma-separated key-value pairs within curly braces.

Basic Syntax and Examples

The simplest dictionary literal is an empty pair of braces. This creates a dictionary with no items.


# Creating an empty dictionary
empty_dict = {}
print(empty_dict)
print(type(empty_dict))
    

{}
<class 'dict'>
    

To create a dictionary with data, place key-value pairs inside. A colon : separates each key from its value. Commas separate each pair.


# A simple dictionary literal
person = {"name": "Alice", "age": 30, "city": "London"}
print(person)
    

{'name': 'Alice', 'age': 30, 'city': 'London'}
    

Key Rules for Dictionary Literals

Understanding the rules for keys is crucial. Dictionary keys must be hashable and immutable. Common key types are strings, numbers, and tuples.


# Valid keys: strings, integers, tuples
valid_dict = {
    "name": "String Key",
    42: "Integer Key",
    (1, 2): "Tuple Key"
}
print(valid_dict)
    

{'name': 'String Key', 42: 'Integer Key', (1, 2): 'Tuple Key'}
    

Lists and other dictionaries are mutable. They cannot be used as keys. Attempting to do so will cause a TypeError.


# INVALID: Using a list as a key will cause an error
# invalid_dict = {["a", "b"]: "value"}  # TypeError: unhashable type: 'list'
    

Values can be of any data type. They can be strings, numbers, lists, other dictionaries, or even functions.

Creating Nested Dictionaries

Dictionary literals are powerful for nesting. You can place a dictionary as a value inside another dictionary. This creates complex, hierarchical data structures.


# A nested dictionary literal
company = {
    "name": "TechCorp",
    "employees": {
        "engineering": 50,
        "sales": 20
    },
    "offices": ["New York", "Berlin"]
}
print(company["employees"]["engineering"])
print(company["offices"][0])
    

50
New York
    

Dictionary Literal vs dict() Constructor

Python provides two main ways to create dictionaries: the literal syntax and the dict() constructor. The literal syntax is generally preferred for its readability and speed.


# Using dict() constructor
person_constructor = dict(name="Bob", age=25)
print(person_constructor)

# Using dictionary literal (often faster and clearer)
person_literal = {"name": "Bob", "age": 25}
print(person_literal)
    

{'name': 'Bob', 'age': 25}
{'name': 'Bob', 'age': 25}
    

The literal syntax is more versatile. It allows any hashable key type. The dict() constructor with keyword arguments only accepts string keys.

Common Operations with Dictionary Literals

Once created, you can access, modify, and delete items. Access is done using square brackets and the key.


config = {"theme": "dark", "version": 2.1}
# Access a value
print(config["theme"])

# Modify a value
config["version"] = 3.0
print(config)

# Add a new key-value pair
config["language"] = "Python"
print(config)

# Delete a key-value pair
del config["theme"]
print(config)
    

dark
{'theme': 'dark', 'version': 3.0}
{'theme': 'dark', 'version': 3.0, 'language': 'Python'}
{'version': 3.0, 'language': 'Python'}
    

To check if a dictionary has no items, you can use techniques discussed in our guide on how to Check if Python Dictionary is Empty.

Merging Dictionary Literals

You can combine dictionaries in several ways. In Python 3.9+, the merge operator | provides a clean method.


dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "b": 99}  # Note: key 'b' is in both

# Merge with | (Python 3.9+). dict2's values take precedence.
merged = dict1 | dict2
print(merged)
    

{'a': 1, 'b': 99, 'c': 3}
    

For more detailed techniques on combining data structures, see our article on Python Dict Merge: Combine Dictionaries Easily.

Advanced Creation: Dict Comprehensions

Dictionary literals can be created dynamically using comprehensions. This is a concise way to transform data. It is similar to list comprehensions but produces a dictionary.


# Create a dictionary of squares
squares = {x: x**2 for x in range(5)}
print(squares)

# Create a dictionary from two lists
keys = ['a', 'b', 'c']
values = [1, 2, 3]
mapped_dict = {k: v for k, v in zip(keys, values)}
print(mapped_dict)
    

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
{'a': 1, 'b': 2, 'c': 3}
    

For a deep dive into this powerful feature, explore our Python Dict Comprehension Guide & Examples.

Conclusion

The Python dictionary literal is a fundamental and elegant tool. Its syntax with curly braces and colons is simple yet powerful. It allows for the creation of everything from simple mappings to complex nested data.

Remember the key rules: keys must be hashable. Use the literal syntax for most cases due to its clarity and performance. Combine it with operations like merging and comprehensions for advanced data manipulation.

Mastering dictionary literals is a key step in becoming proficient with Python's data structures. It forms the basis for working with JSON-like data, configuration settings, and efficient lookups in your programs.