The Python Dictionary (dict) is a very important data type that stores data in Key-Value pairs. You use it to associate a value like "Tanaka" with a key like "name", or a value 101 with a key "id". While lists manage data using indices (0, 1, 2…), dictionaries manage data using unique keys instead of indices.
This article explains three basic ways to create dictionaries.
1. Using {} (Curly Braces) – Literal Syntax
The most common and intuitive method is the literal syntax using {} (curly braces).
Specifying Key-Value Pairs
Write the dictionary by separating keys and values with a colon (:), and separating each pair with a comma (,), like {key1: value1, key2: value2}.
# Define user configuration as a dictionary
user_config = {
"username": "admin-001",
"theme": "dark",
"notify_email": True,
"login_count": 25
}
print(f"Created Dictionary: {user_config}")
print(f"Type: {type(user_config)}")
Output:
Created Dictionary: {'username': 'admin-001', 'theme': 'dark', 'notify_email': True, 'login_count': 25}
Type: <class 'dict'>
Creating an Empty Dictionary
Writing only {} (curly braces) creates a dictionary with no contents.
empty_storage = {}
print(f"Empty Dictionary: {empty_storage}")
print(f"Type: {type(empty_storage)}")
Output:
Empty Dictionary: {}
Type: <class 'dict'>
Important Note: Writing {} creates an empty dictionary (dict). If you want to create an empty set, you must use the set() constructor instead. This is one of the most common mistakes between sets and dicts.
2. Using the dict() Constructor
You can also create dictionaries using the built-in dict() constructor (which acts like a function).
Creating with Keyword Arguments (key=value)
If your keys are strings (and valid Python variable names), you can pass them as keyword arguments like dict(key=value). This method allows you to write keys without quotes (" or '), which can make the code cleaner.
# Create a dictionary using keyword arguments
server_settings = dict(
host="192.168.1.1",
port=5432,
database="main_db"
)
print(f"Settings: {server_settings}")
Output:
Settings: {'host': '192.168.1.1', 'port': 5432, 'database': 'main_db'}
Creating from a List of (Key, Value) Pairs (Iterable)
This method is useful when generating a dictionary from other data formats (e.g., a list of tuples). You pass a list containing multiple (key, value) tuples to dict().
# List of (Key, Value) tuples
data_pairs = [
("item_code", "A-901"),
("quantity", 150),
("location", "Warehouse-C")
]
# Convert list to dictionary
stock_info = dict(data_pairs)
print(f"Stock Info (Dict): {stock_info}")
Output:
Stock Info (Dict): {'item_code': 'A-901', 'quantity': 150, 'location': 'Warehouse-C'}
Creating an Empty Dictionary with dict()
Calling dict() without arguments creates an empty dictionary. This is equivalent to {}.
empty_dict_constructor = dict()
print(f"Empty Dictionary (dict()): {empty_dict_constructor}")
Summary
We introduced three ways to create dictionaries in Python.
{key: value}(Literal Syntax): The most common method. Can be used with any valid key type (strings, numbers, etc.).dict(key=value)(Keyword Arguments): Useful for writing code concisely when keys are simple strings.dict([(key, val), ...])(Iterable): Used to dynamically generate dictionaries from other data structures like lists or tuples.- To create an empty dictionary,
{}is generally used.
