Day 15 – Master Dictionaries in Python – Basics & Operations | 60 Days Python Crash Course | ElectroLab

Dictionaries in Python – Basics & Operations
Introduction
Dictionaries in Python are one of the most powerful and flexible data types. They allow you to store data in key-value pairs, enabling fast access and efficient data organization. Whether you’re building an app, managing a database, or just storing structured data, dictionaries are your go-to data structure in Python. In today’s lesson, we will explore every essential detail you need to understand and effectively use dictionaries in your Python projects.
What You’ll Learn Today:
- What are dictionaries in Python?
- Creating and accessing dictionary elements
- Dictionary methods and operations
- Nested dictionaries
- Iterating through dictionaries
- Dictionary comprehension
- Common mistakes to avoid
- Real-life use cases
- Quiz to test your understanding
- FAQs and summary
Understanding Dictionaries in Python

A dictionary in Python is an unordered, mutable collection of items. Each item is stored as a pair: key and value. Keys are unique and must be of an immutable data type (like strings, numbers, or tuples). Values, on the other hand, can be of any data type and can be duplicated.
Creating a Dictionary
student = {
"name": "John",
"age": 21,
"course": "Python"
}
Accessing Elements
print(student["name"]) # Output: John
You can also use the get()
method to safely access elements:
print(student.get("grade", "Not Available"))
Modifying Elements
student["age"] = 22
Adding New Key-Value Pair
student["grade"] = "A"
Removing Elements
del student["course"]
Dictionary Methods and Operations in Python

Example:
info = {"name": "Alice", "city": "Delhi"}
info.update({"city": "Mumbai", "email": "alice@mail.com"})
print(info)
Nested Dictionaries
Dictionaries can contain other dictionaries as values. This is useful when managing structured data.
students = {
"101": {"name": "Rahul", "grade": "A"},
"102": {"name": "Sneha", "grade": "B"}
}
print(students["101"]["name"]) # Output: Rahul
Using Dictionaries in a Loop
person = {"name": "David", "age": 25, "job": "Engineer"}
for key, value in person.items():
print(key, ":", value)
Dictionary Comprehension
Like list comprehensions, you can also create dictionaries in python in one line:
squares = {x: x*x for x in range(1, 6)}
print(squares)
# Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Common Mistakes to Avoid
- Using mutable keys like lists – keys must be immutable (e.g., strings, numbers, tuples).
- Accessing non-existent keys without
.get()
can raise KeyError. - Assuming dictionaries are ordered before Python 3.7.
- Copying dictionaries improperly – use
.copy()
to avoid reference issues. - Misunderstanding
.update()
– it overwrites existing keys.
Real-life Use Case of Dictionaries
Imagine managing student records in a school:
student_db = {
"001": {"name": "Amit", "marks": 89},
"002": {"name": "Neha", "marks": 95},
"003": {"name": "Ravi", "marks": 78}
}
# Fetch Neha's marks
print(student_db["002"]["marks"])
Dictionaries help structure data clearly and allow fast data retrieval using unique keys.
Quiz – Test Your Knowledge
- What is the correct syntax to access the value of key “name” in a dictionary
person
? - Which method safely retrieves a value without raising an error if the key doesn’t exist?
- What will be the output of
list(person.keys())
? - How can you iterate through key-value pairs in a dictionary?
- Write a dictionaries in python comprehension to map numbers 1 to 5 with their cubes.
FAQs
Q1. Can a dictionary have duplicate keys?
No, keys in a dictionary must be unique.
Q2. Can we use a list as a dictionary key?
No, keys must be immutable types like strings, numbers, or tuples.
Q3. How to merge two dictionaries in Python?
Use the
.update()
method or unpack them using{**dict1, **dict2}
.
Q4. What is the time complexity of dictionary lookup?
Dictionary lookups are generally O(1), making them very efficient.
Summary
Recap:
- Dictionaries store data as key-value pairs.
- Keys must be unique and immutable.
- Supports a wide range of built-in methods.
- Nested dictionaries allow multi-level structured data.
- Dictionary comprehension offers concise creation.
- Practical in real-world use cases like databases and configurations.
External Resources
Learn More with ElectroLab.in
- What is Python and How to Install
- First Python Program – Print Function
- Variables and Data Types
- Typecasting and Input Function
- Arithmetic, Comparison & Logical Operators
- String Manipulation & Methods
- String Formatting & Escape Sequences
- If-Elif-Else Conditions
- For Loops in Python
- While Loop in Python
- Break, Continue and Pass
- Learn about Lists in Python
- Explore Tuples in Python
- Sets in Python
What’s Next?
In the next lesson, we’ll dive into functions in Python. You’ll learn how to define and use functions, pass arguments, return values, and write reusable code.
Keep exploring and coding daily. You’re building solid foundations for advanced Python topics ahead.