DICTIONARY – COMPLETE PYTHON NOTES
1. What is a Dictionary?
A dictionary is a collection of key–value pairs. Keys map to values.
2. Creating a Dictionary
• person = {"name": "Aarav", "city": "Delhi", "age": 20}
• info = dict(name="Aarav", city="Delhi", age=20)
• d = {} and then d["language"] = "Python"
3. Properties of Dictionaries
1. Unordered (preserves insertion order)
2. Key–value based structure
3. Keys must be unique
4. Keys must be immutable
5. Mutable structure
6. Fast lookup (O(1))
7. Dynamic size
4. Accessing Values (5 Methods)
• get(key)
• keys()
• values()
• items()
• Direct access: dict[key]
5. Dictionary Methods
1. get(key, default)
Example:
data = {"a":1, "b":2}
[Link]("c", "Not Found")
2. keys()
Returns all keys.
3. values()
Returns all values.
4. items()
Returns (key, value) pairs.
5. setdefault(key, default)
Adds key if not exists.
6. popitem()
Removes last key-value pair.
7. update()
Merges dictionaries.
8. clear()
Removes all items.
9. copy()
Creates a shallow copy.
6. Example Program
student = {
"name": "Rakshita",
"age": 18,
"skills": ["Python", "Web Dev"]
print([Link]("name"))
student["city"] = "Karnal"
[Link]({"age": 19})
[Link]()
print([Link]())
print([Link]())
print([Link]())