Python Foundation
Dictionaries in Python
What is a Dictionary?
A dictionary is a collection of key-value pairs.
Each key is unique and is used to access its corresponding value.
Python
student = {
"name": "Kundan",
"age": 25,
"course": "Data Science"
}
print(student)
print(type(student))
Output
{'name': 'Kundan', 'age': 25, 'course': 'Data Science'}
<class 'dict'>
Key Characteristics
- Stores data in key : value format
- Keys must be unique and immutable (string, number, tuple)
- Values can be of any data type
- Ordered (from Python 3.7+)
- Mutable
Creating Dictionaries
Using Curly Braces
Python
person = {
"name": "Ravi",
"age": 28,
"city": "Hyderabad"
}
print(person)
Using dict() Constructor
Python
person2 = dict(name="Priya", age=24, city="Mumbai")
print(person2)
empty_dict = {}
print(type(empty_dict))
Output
{'name': 'Priya', 'age': 24, 'city': 'Mumbai'}
<class 'dict'>
Accessing Dictionary Items
Python
student = {
"name": "Kundan",
"age": 25,
"course": "Data Science",
"city": "Hyderabad"
}
print(student["name"])
print(student.get("age"))
print(student.get("grade", "Not Available")) # Default value
Output
Kundan 25 Not Available
Using .get() is safer because it does not raise an error if the key is missing.
Adding and Updating Items
Python
student = {
"name": "Kundan",
"age": 25
}
# Adding new key-value
student["course"] = "Data Science"
print(student)
# Updating existing value
student["age"] = 26
print(student)
# Using update()
student.update({"city": "Hyderabad", "age": 27})
print(student)
Output
{'name': 'Kundan', 'age': 25, 'course': 'Data Science'}
{'name': 'Kundan', 'age': 26, 'course': 'Data Science'}
{'name': 'Kundan', 'age': 27, 'course': 'Data Science', 'city': 'Hyderabad'}
Removing Items from Dictionary
Python
student = {
"name": "Kundan",
"age": 25,
"course": "Data Science",
"city": "Hyderabad"
}
student.pop("age") # Remove specific key
print(student)
student.popitem() # Remove last inserted item
print(student)
del student["course"]
print(student)
student.clear() # Remove all items
print(student)
Output
{'name': 'Kundan', 'course': 'Data Science', 'city': 'Hyderabad'}
{'name': 'Kundan', 'course': 'Data Science'}
{'name': 'Kundan'}
{}
Important Dictionary Methods
Python
student = {
"name": "Kundan",
"age": 25,
"course": "Data Science"
}
print(student.keys()) # All keys
print(student.values()) # All values
print(student.items()) # All key-value pairs
print("name" in student)
print(len(student))
Output
dict_keys(['name', 'age', 'course'])
dict_values(['Kundan', 25, 'Data Science'])
dict_items([('name', 'Kundan'), ('age', 25), ('course', 'Data Science')])
True
3
Looping Through a Dictionary
Python
student = {
"name": "Kundan",
"age": 25,
"course": "Data Science"
}
# Loop through keys
for key in student:
print(key)
print("-----")
# Loop through keys and values
for key, value in student.items():
print(key, ":", value)
Output
name age course ----- name : Kundan age : 25 course : Data Science
Nested Dictionaries
A dictionary can contain another dictionary as its value.
Python
students = {
"student1": {
"name": "Kundan",
"age": 25
},
"student2": {
"name": "Priya",
"age": 24
}
}
print(students["student1"]["name"])
print(students["student2"]["age"])
Output
Kundan 24
Summary – Dictionaries in Python
- Dictionary stores data as key-value pairs.
- Created using
{ }ordict(). - Keys must be unique and immutable.
- Access values using
dict[key]ordict.get(key). - Important methods:
keys(),values(),items(),update(),pop(),clear() - Dictionaries are widely used for structured data.
You have completed the core Python data structures!
Dictionaries in Python
Learn how to store and manage data using key-value pairs.
Select any topic from the left sidebar to begin.
Start Learning →
