Here is a clear, clear, and exam-ready explanation of Dictionary in Python, perfect for BCA/MCA/B.Tech students.
Dictionary in Python
A dictionary in Python is an unordered, mutable collection of key–value pairs.
It is one of the most important data types in Python and is used when data needs to be stored in a structured way, like a real dictionary (word → meaning).
1. Features of a Dictionary
✔ Stores data in key : value format
✔ Keys must be unique
✔ Keys must be immutable (str, int, tuple)
✔ Values can be any data type
✔ Dictionary is mutable (can be changed)
✔ Unordered (in Python 3.7+, preserves insertion order)
2. Creating a Dictionary
Basic Dictionary
student = {
"name": "John",
"age": 20,
"course": "BCA"
}
Empty Dictionary
d = {}
Using dict()
d = dict(name="John", age=20)
3. Accessing Dictionary Values
Use the key inside square brackets:
print(student["name"]) # John
OR use .get() method (safer):
print(student.get("age")) # 20
If key does not exist:
student["x"]→ Error (KeyError)student.get("x")→ Returns None
4. Modifying Dictionary
Add or update values
student["age"] = 21
student["city"] = "Delhi" # New key-value pair
5. Removing Elements
| Method | Description |
|---|---|
pop(key) | Removes item by key |
popitem() | Removes last inserted item |
del dict[key] | Deletes specific key |
clear() | Removes all items |
Example:
student.pop("age")
del student["course"]
student.clear()
6. Looping Through a Dictionary
Loop through keys
for k in student:
print(k)
Loop through values
for v in student.values():
print(v)
Loop through both keys and values
for k, v in student.items():
print(k, v)
7. Dictionary Methods (Important for Exams)
| Method | Description |
|---|---|
keys() | Returns all keys |
values() | Returns all values |
items() | Returns key–value pairs |
get(key) | Returns value |
update(dict) | Adds/updates multiple items |
pop(key) | Removes item |
popitem() | Removes last item |
clear() | Clears dictionary |
fromkeys() | Creates dictionary from keys |
8. Checking Membership
Check if a key exists:
"name" in student
Check if a value exists:
"John" in student.values()
9. Nested Dictionary
A dictionary inside another dictionary.
students = {
"101": {"name": "John", "age": 20},
"102": {"name": "Amit", "age": 22}
}
print(students["101"]["name"]) # John
10. Dictionary Comprehension
Just like list comprehension.
Example:
squares = {x: x*x for x in range(1, 6)}
print(squares)
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
11. Example Programs
Example 1: Merge two dictionaries
a = {"x": 1, "y": 2}
b = {"y": 3, "z": 4}
a.update(b)
print(a)
Example 2: Count frequency of elements
items = ["a", "b", "a", "c", "b", "a"]
freq = {}
for item in items:
freq[item] = freq.get(item, 0) + 1
print(freq)
Output:
{'a': 3, 'b': 2, 'c': 1}
12. Summary Table: Dictionary
| Property | Description |
|---|---|
| Ordered | ✔ Yes (Python 3.7+) |
| Mutable | ✔ Yes |
| Duplicate keys | ❌ No |
| Duplicate values | ✔ Yes |
| Keys | Must be immutable |
| Values | Any data type |
| Syntax | { key: value } |
