|
| Home |
|---|
A Python dictionary is a built-in data structure used to store data in the form of key-value pairs. Each key is associated with a specific value, allowing Python programs to quickly retrieve and update data.
Dictionaries are extremely useful when working with structured data such as student records, configuration settings, JSON data, API responses, and database-like information.
Unlike lists, which use numeric indexes, dictionaries use keys to access values.
A dictionary is created using curly brackets { }.
Each element contains a key and a value separated by a colon :.
Multiple key-value pairs are separated by commas.
student = {
"name": "Alice",
"age": 21,
"course": "Python"
}
print(student)
{'name': 'Alice', 'age': 21, 'course': 'Python'}
In this example:
Dictionaries are often used to store structured records.
student = {
"name": "John",
"age": 22,
"major": "Computer Science",
"university": "MIT"
}
print(student["name"])
print(student["major"])
John
Computer Science
product = {
"id": 101,
"name": "Laptop",
"price": 1200,
"in_stock": True
}
print(product)
{'id': 101, 'name': 'Laptop', 'price': 1200, 'in_stock': True}
Each element in a dictionary consists of a key and its corresponding value.
person = {
"name": "Sarah",
"city": "London"
}
print(person)
Dictionary keys must be unique. If duplicate keys exist, Python keeps the last value assigned to that key.
data = {
"name": "Alice",
"name": "Bob"
}
print(data)
{'name': 'Bob'}
Dictionaries can be modified after creation. You can update values, add new keys, or remove items.
student = {
"name": "Alice",
"age": 21
}
student["age"] = 22
print(student)
{'name': 'Alice', 'age': 22}
Modern versions of Python preserve the order of insertion. This means dictionary elements appear in the order they were added.
data = {
"a": 1,
"b": 2,
"c": 3
}
print(data)
Dictionary values can contain different data types such as numbers, strings, lists, or even other dictionaries.
data = {
"name": "Alice",
"age": 21,
"skills": ["Python","SQL","Machine Learning"]
}
print(data)
Python dictionaries are widely used in real programming tasks because they allow efficient lookup and structured data storage.
Now that you understand what a Python dictionary is, the next tutorial explains how to create dictionaries using different techniques.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net