Understanding Python Dictionary Literals
Python dictionaries are used to store related data as key-value pairs, such as student records, employee details, product information, or settings. These values can be written directly in code using a dictionary literal or created during program execution.
In this guide, you’ll learn what Python dictionary literals are, their syntax, how they work, and how to use them with simple examples.
🚀 Getting Started: Before focusing on dictionaries, explore our guide on Python literals to understand how different literal types work in Python.
💡 Tip: To understand how Python stores and works with dictionaries internally, you can explore our guide on Python dict() function
Introduction: What is a Dictionary Literal?
Definition: A Python dictionary literal is a collection of one or more key-value pairs written directly in Python code and enclosed within curly braces ({}).
Each key is separated from its corresponding value by a colon (:), while individual key-value pairs are separated by commas. Dictionary keys must be immutable, whereas values can be any valid Python object.
Example: In the statement student_scores = {"Alice": 85, "Bob": 90}, the value {"Alice": 85, "Bob": 90} is a dictionary literal because it is written directly in the code and enclosed within curly braces.
Before exploring the different forms of Python dictionary literals, let’s first understand their syntax.
How to Write Python Dictionary Literals
Python allows dictionary literals to be written directly in the source code without using any special function or constructor.
A dictionary literal is created by enclosing one or more key-value pairs within curly braces, separating each key from its value with a colon and separating individual pairs with commas.
The syntax below shows the general structure of a Python dictionary literal.
Syntax
dictionary_name = {key1: value1, key2: value2, key3: value3, ...}Explanation: Here, dictionary_name is the variable used to store the dictionary. Each key is associated with a value using a colon (:), multiple key-value pairs are separated by commas, and the entire collection is enclosed within curly braces.
| Component | Description |
|---|---|
dictionary_name | The name of the variable used to store the dictionary. |
= | The assignment operator used to assign the dictionary literal to a variable. |
{ } | Curly braces that define the dictionary literal. |
key : value | A key-value pair where the key identifies the value. Keys must be immutable, while values may be of the same or different data types. |
, | Separates one key-value pair from another. |
Let’s explore the different forms of Python dictionary literals with simple examples.
Forms of Python Dictionary Literals
Python dictionary literals can be written in different forms depending on the type of keys and values they contain:
Let’s explore each form of Python dictionary literal with explanations and examples.
1. String Keys with Integer Values
Dictionary literals commonly use strings as keys and integers as values to store related information.
Example: String Keys with Integer Values
# Dictionary with string keys and integer values
student_scores = {
"Alice": 85,
"Bob": 90,
"Charlie": 78
}
print(student_scores)
# Output
{'Alice': 85, 'Bob': 90, 'Charlie': 78}
Explanation: Each student’s name acts as a unique key, while the corresponding score is stored as its value. Dictionary keys allow values to be retrieved quickly using their associated key.
2. Mixed Value Types
Dictionary values can be of different data types within the same dictionary.
Example: Dictionary with Mixed Value Types
# Dictionary with mixed value types
person_info = {
"name": "John",
"age": 30,
"height": 5.9,
"is_student": True
}
print(person_info)
# Output
{'name': 'John', 'age': 30, 'height': 5.9, 'is_student': True}
Explanation: This dictionary stores a string, an integer, a floating-point number, and a Boolean value together. Python dictionaries allow values of different data types to exist within the same collection.
3. Tuple as Dictionary Keys
Dictionary keys must be immutable. Since tuples are immutable, they can be used as dictionary keys.
Example: Tuple as Dictionary Keys
# Dictionary with tuple keys
coordinate_dict = {
("x", "y"): (10, 20),
("a", "b"): (30, 40)
}
print(coordinate_dict)
# Output
{('x', 'y'): (10, 20), ('a', 'b'): (30, 40)}
Explanation: The tuple ("x", "y") and ("a", "b") are used as dictionary keys, while the corresponding tuple values store coordinate pairs. Immutable objects such as tuples can safely be used as dictionary keys.
Key Examples at a Glance: Dictionary Literals
The following table summarizes the different forms of Python dictionary literals:
| Type | Format | Example | Meaning |
|---|---|---|---|
| String Keys with Integer Values | {key: value} |
{"Alice": 85, "Bob": 90} |
Maps string keys to integer values. |
| Mixed Value Types | {key: value} |
{"name": "John", "age": 30} |
Stores values of different data types. |
| Tuple as Dictionary Keys | {(key1, key2): value} |
{("x", "y"): (10, 20)} |
Uses immutable tuples as dictionary keys. |
Key Takeaways: Dictionary Literals
Here are the key points to remember about Python dictionary literals:
- A Python dictionary literal is created using curly braces (
{}). - Dictionary data is stored as key-value pairs separated by a colon (
:). - Each key in a dictionary must be unique and immutable.
- Dictionary values can be of the same or different data types.
- Dictionaries preserve the insertion order of key-value pairs.
- Immutable objects such as tuples can be used as dictionary keys.