Skip to content

Python Create Dictionaries

Syntax

Dictionaries in Python are collections of key-value pairs, defined using curly braces {}.

# Creating an empty dictionary
empty_dict = {}
# Creating a dictionary with key-value pairs
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# Accessing dictionary values by key
name = person["name"]
age = person["age"]

Example

# Creating a dictionary to store product information
product = {
"name": "Laptop",
"price": 999.99,
"brand": "TechCo"
}
print(f"Product: {product['name']}, Price: ${product['price']}, Brand: {product['brand']}")