Chapter-9, M3-R5
Dictionary in Python
1. Introduction to Dictionary
English:
A dictionary is an unordered, mutable collection of items where each item is stored
as a key–value pair.
Keys must be unique and immutable (string, number, tuple).
Values can be any type.
Hindi:
Dictionary Python की एक डेटा स्ट्रक्चर है जिसमें डेटा key–value pair के रूप में store होता है।
Key हमेशा unique और immutable होती है ।
Value ककसी भी data type की हो सकती है ।
2. Syntax of Dictionary
dictionary_name = {
key1: value1,
key2: value2,
key3: value3
}
**3. Why Use Dictionary?
(डडक्शनरी क्यों उपयोग करें ?)**
Data को key से access करना fast होता है
Structure real-world िैसा (िैसे Aadhaar database)
Mahesh Srivastava 9839263360 Page 1
Flexible and powerful
No index required
Search and update fast
4. Characteristics of Dictionary
Feature Description
Ordered Since Python 3.7
Mutable Change allowed
Unique Keys Duplicate keys not allowed
Dynamic Size can grow or shrink
Heterogeneous Different types allowed
5. Creating Dictionary (Ways)
(1) Using { }
d = {"id": 101, "name": "Rahul"}
(2) Using dict()
d = dict(city="Delhi", pincode=110092)
(3) Empty Dictionary
d = {}
(4) Using list of tuples
d = dict([("a", 10), ("b", 20)])
(5) Using fromkeys()
d = [Link](["a", "b", "c"], 0)
6. Access Elements
Using key
print(d["name"])
Using get()
Mahesh Srivastava 9839263360 Page 2
print([Link]("city"))
➡ get() does NOT give error if key not found.
7. Dictionary Functions (Very
Important)
Function Output
len(d) number of items
[Link]() returns all keys
[Link]() returns all values
[Link]() returns all (key, value) pairs
[Link](key) returns value
[Link](key) removes key
[Link]() removes last inserted
[Link]() remove all items
[Link]() shallow copy
[Link](d2) merge/update
8. Update and Modify Dictionary
Add new pair
d["age"] = 25
Update existing
d["age"] = 30
Using update()
[Link]({"city": "Mumbai"})
9. Deleting Items
Remove by key
[Link]("name")
Delete last item
[Link]()
Mahesh Srivastava 9839263360 Page 3
Delete complete dictionary
[Link]() # empty
del d # remove entire dictionary
10. Looping in Dictionary
Loop keys
for k in d:
print(k)
Loop values
for v in [Link]():
print(v)
Loop keys + values
for k, v in [Link]():
print(k, v)
**11. Nested Dictionary
(डडक्शनरी के अंदर डडक्शनरी)**
student = {
"name": "Amit",
"marks": {
"math": 88,
"science": 92
}
}
print(student["marks"]["science"])
**12. Dictionary Comprehension
(Short-form dictionary)**
squares = {x: x*x for x in range(1, 6)}
Output
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Mahesh Srivastava 9839263360 Page 4
13. Important Notes (Exam Points)
✔ Key must be unique
✔ Key must be immutable
✔ Value can be mutable
✔ Dictionary is fast
✔ Use get() to avoid KeyError
✔ popitem() removes last item (Python 3.7+)
✔ update() merges dictionaries
14. Real Life Uses of Dictionary
Student records
Employee database
Contacts list
JSON data
API response
Login username/password storage
Settings / configuration files
15. BIG Example Project (Useful for
Lab Practical)
student = {
"roll": 101,
"name": "Mahesh",
"marks": {
"math": 78,
"science": 88,
"english": 90
},
"courses": ["Python", "HTML", "CSS"]
}
print("Name =", student["name"])
print("Math Marks =", student["marks"]["math"])
print("All Courses =", student["courses"])
Mahesh Srivastava 9839263360 Page 5
✅ LAB WORK – 1
Create a dictionary and display all keys & values.
Code:
student = {"name": "Rahul", "age": 20, "course": "Python"}
print("Keys:", [Link]())
print("Values:", [Link]())
Output:
Keys: dict_keys(['name', 'age', 'course'])
Values: dict_values(['Rahul', 20, 'Python'])
✅ LAB WORK – 2
Add a new item and update an existing item.
Code:
data = {"id": 101, "name": "Amit"}
data["city"] = "Delhi" # Add
data["name"] = "Amit Kumar" # Update
print(data)
Output:
{'id': 101, 'name': 'Amit Kumar', 'city': 'Delhi'}
✅ LAB WORK – 3
Delete a dictionary element using pop() and popitem().
Code:
d = {"a": 10, "b": 20, "c": 30}
[Link]("b")
print("After pop:", d)
[Link]()
print("After popitem:", d)
Mahesh Srivastava 9839263360 Page 6
Output:
After pop: {'a': 10, 'c': 30}
After popitem: {'a': 10}
✅ LAB WORK – 4
Access a value using key and get().
Code:
emp = {"name": "Mahesh", "salary": 25000}
print(emp["salary"])
print([Link]("age"))
Output:
25000
None
✅ LAB WORK – 5
Create a nested dictionary and print inner values.
Code:
student = {
"name": "Riya",
"marks": {
"math": 85,
"science": 90
}
}
print(student["marks"]["science"])
Output:
90
✅ LAB WORK – 6
Traverse a dictionary using for loop.
Mahesh Srivastava 9839263360 Page 7
Code:
d = {"a": 1, "b": 2, "c": 3}
for k, v in [Link]():
print(k, "=", v)
Output:
a = 1
b = 2
c = 3
✅ LAB WORK – 7
Check if a key exists in dictionary.
Code:
d = {"id": 10, "age": 25}
if "age" in d:
print("Key Found")
else:
print("Key Not Found")
Output:
Key Found
✅ LAB WORK – 8
Convert two lists into a dictionary.
Code:
keys = ["name", "age", "city"]
values = ["Rohan", 22, "Mumbai"]
d = dict(zip(keys, values))
print(d)
Output:
{'name': 'Rohan', 'age': 22, 'city': 'Mumbai'}
Mahesh Srivastava 9839263360 Page 8
✅ LAB WORK – 9
Find maximum and minimum value from a dictionary.
Code:
d = {"a": 40, "b": 10, "c": 90, "d": 20}
print("Max =", max([Link]()))
print("Min =", min([Link]()))
Output:
Max = 90
Min = 10
✅ LAB WORK – 10
Make a dictionary using comprehension.
Code:
square = {x: x*x for x in range(1, 6)}
print(square)
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Mahesh Srivastava 9839263360 Page 9
100 MCQs – Python Dictionary (With
Options + Answers)
1. What is a dictionary in C. {"a":1, "b":2}
D. ("a":1, "b":2)
Python? Answer: C
A. Ordered collection of elements
B. Unordered collection of key-value pairs
C. Collection of duplicate keys
D. Collection of values only
5. Can a dictionary have
Answer: B duplicate keys?
A. Yes
B. No
C. Sometimes
2. Which symbol is used to D. Only in Python 2
create a dictionary? Answer: B
A. []
B. ()
C. {}
D. <>
6. Which method returns
Answer: C all keys?
A. all()
B. keys()
3. What are dictionary C. getkeys()
D. values()
keys? Answer: B
A. Mutable objects
B. Immutable objects
C. List objects
D. Only integers
7. Which method returns
Answer: B all values?
A. vals()
B. values()
4. Which of the following C. items()
D. keyvalues()
is a correct dictionary? Answer: B
A. {1, 2, 3}
B. ["a":1, "b":2]
Mahesh Srivastava 9839263360 Page 10
8. Which function returns 12. Which method is used
number of items? to remove an item by key?
A. length() A. delete()
B. size() B. remove()
C. len() C. pop()
D. count() D. discard()
Answer: C Answer: C
9. Which method returns 13. popitem() removes:
key-value pairs? A. First item
B. Middle item
A. items()
C. Last inserted item
B. pairs()
D. Random item
C. keyvalue()
Answer: C
D. join()
Answer: A
14. Which method clears
10. Which operator checks all items?
if key exists? A. delete()
B. clear()
A. in
C. empty()
B. exist
D. remove()
C. find
Answer: B
D. include
Answer: A
15. Which creates an
11. What is the output of: empty dictionary?
d = {"a":1, "b":2} A. []
print(d["a"]) B. {}
C. dict()
A. a D. Both B and C
B. 1 Answer: D
C. Error
D. ["a"]
Answer: B
16. Keys in dictionary
must be:
Mahesh Srivastava 9839263360 Page 11
A. Mutable A. d["key"]
B. Immutable B. [Link]("key")
C. Integer only C. [Link]()
D. None D. [Link]()
Answer: B Answer: A
17. Values in dictionary 21. Which adds new key-
can be: value pair?
A. Only strings A. add()
B. Only numbers B. insert()
C. Any data type C. d[key] = value
D. Only list D. push()
Answer: C Answer: C
18. What is output? 22. Which merges two
d = {"a":10,"a":20}
dictionaries?
print(d["a"])
A. merge()
A. 10 B. join()
B. 20 C. update()
C. Both D. append()
D. Error Answer: C
Answer: B
23. Output?
19. To access value safely,
d = {1: "a", 2: "b"}
we use: print(len(d))
A. check() A. 1
B. safe() B. 2
C. get() C. 3
D. find() D. Error
Answer: C Answer: B
20. Which of these gives 24. Dictionary is:
error? A. Mutable
B. Immutable
Mahesh Srivastava 9839263360 Page 12
C. Static D. (1,2)
D. None Answer: C
Answer: A
29. Which is mutable?
25. Which returns list of
keys? A. key
B. value
C. both
A. keylist()
D. none
B. list(d)
Answer: B
C. [Link]()
D. allkeys()
Answer: C
30. Which creates nested
dictionary?
26. Which converts
dictionary to list of keys? A. {1:(1,2)}
B. {1:[1,2]}
C. {1:{2:3}}
A. list(d)
D. None
B. [Link]()
Answer: C
C. [Link]()
D. values()
Answer: A
31. What is output?
d = {"x":5, "y":10}
27. What is output? print("x" in d)
d = {} A. False
print(type(d)) B. True
C. Error
A. list D. None
B. dict Answer: B
C. set
D. tuple
Answer: B
32. What does [Link]() return?
A. List of keys
28. Which is NOT valid B. List of values
C. List of key-value tuples
key? D. None
Answer: C
A. 1
B. "name"
C. [1,2,3]
Mahesh Srivastava 9839263360 Page 13
33. Which method copies a A. Keys can be lists
dictionary? B. Keys can be tuples
C. Keys must be mutable
A. duplicate() D. Keys must be float
B. copy() Answer: B
C. clone()
D. duplicateDict()
Answer: B
38. Which operator merges
dictionaries in Python 3.9+ ?
34. What is output? A. +
B. -
d = {1: "A", 2: "B"} C. |
print([Link](3)) D. &
Answer: C
A. A
B. B
C. None
D. Error 39. Which method returns default
Answer: C
value if key not found?
A. get()
B. find()
35. Which removes all items? C. value()
D. key()
A. pop() Answer: A
B. remove()
C. delete()
D. clear()
Answer: D
40. Output?
d = {1:10, 2:20, 3:30}
print(max(d))
36. What is output?
A. 10
d = {"a":1, "b":2} B. 30
print([Link]("b")) C. 3
D. Error
A. a Answer: C
B. b
C. 2
D. Error
Answer: C
41. Output?
d = {'a':1,'b':2}
print(len([Link]()))
37. Which is correct?
A. 1
B. 0
Mahesh Srivastava 9839263360 Page 14
C. 2 A. del d
D. Error B. [Link]()
Answer: C C. [Link]()
D. [Link]()
Answer: A
42. Dictionary key must be:
A. Immutable 47. Output?
B. Mutuable
C. Float only d = {1:10,2:20}
D. String only print(2 in d)
Answer: A
A. True
B. False
C. Error
D. None
43. Which changes value of a key?
Answer: A
A. change()
B. modify()
C. d[key] = value
D. setvalue() 48. [Link]() returns:
Answer: C
A. All keys
B. All values
C. All tuples
D. All lists
44. Output?
Answer: B
d = {"a":[1,2]}
print(d["a"][1])
A. a 49. Output?
B. 2
C. [1,2] d = {1:10, 2:20}
D. Error print(10 in [Link]())
Answer: B
A. True
B. False
C. None
D. Error
45. A dictionary is similar to:
Answer: A
A. Set
B. List
C. Real-world dictionary
D. Tuple 50. Which method adds many items
Answer: C at once?
A. add()
B. update()
46. To delete dictionary completely: C. push()
Mahesh Srivastava 9839263360 Page 15
D. insert() 55. To remove unknown key safely:
Answer: B
A. [Link](key)
B. del d[key]
C. [Link](key)
51. Which is correct D. [Link](key, None)
representation? Answer: D
A. {key=value}
B. {key:value}
C. (key:value) 56. Output?
D. [key:value]
Answer: B d = {"x":5}
print([Link]("y", 100))
A. Error
B. 5
52. Nested dictionary means:
C. 100
D. None
A. Dictionary inside list
Answer: C
B. Dictionary inside dictionary
C. List inside dictionary
D. None
Answer: B
57. What does dict() do?
A. Makes list
B. Makes tuple
53. Output?
C. Makes dictionary
d = {"a":1} D. Makes set
d["b"] = d["a"] Answer: C
print(d)
A. {"a":1}
B. {"a":1, "b":1} 58. Which is NOT allowed?
C. Error
D. None A. key = 10
Answer: B B. key = (1,2)
C. key = [1,2]
D. key = "abc"
Answer: C
54. Dictionaries are:
A. Ordered (Python 3.7+)
B. Always unordered 59. Output?
C. Sorted
D. Immutable d = {"a":1,"b":2}
Answer: A print([Link]("c", 3))
A. Error
B. 3
Mahesh Srivastava 9839263360 Page 16
C. None D. Error
D. c Answer: B
Answer: B
64. Which checks if value exists?
60. setdefault() does:
A. in
A. Adds key if not present B. for
B. Removes key C. exists
C. Updates all keys D. None
D. Deletes dictionary Answer: A
Answer: A
65. Output?
61. Which retrieves all key-value
pairs? d = {1:2, 3:4, 5:6}
print(sum([Link]()))
A. all()
A. 10
B. list()
B. 12
C. items()
C. 6
D. pairs()
D. 0
Answer: C
Answer: B
62. Output?
66. Dictionary key cannot be:
d = {"a":1, "b":2}
print(list(d)) A. String
B. Integer
A. ["a","b"] C. Tuple
B. [1,2] D. List
C. [("a",1),("b",2)] Answer: D
D. Error
Answer: A
67. Output?
63. Output? d = {1: [1,2,3]}
print(len(d))
d = {}
d[1] = "A" A. 1
print(d) B. 3
C. Error
A. {} D. 0
B. {1:"A"} Answer: A
C. {"A":1}
Mahesh Srivastava 9839263360 Page 17
68. Output? 72. Output?
d = {1:10} d = {1:10, 2:20}
print(1 not in d) [Link]({3:30})
print(len(d))
A. True
B. False A. 1
C. Error B. 2
D. None C. 3
Answer: B D. Error
Answer: C
69. Dictionary values can be:
73. Output?
A. Only int
B. Only string d = {"a":1}
print([Link]("a",5))
C. Any Python object
D. Only list
A. 1
Answer: C
B. 5
C. Error
D. None
Answer: A
70. Output?
d = {"a":1,"b":2}
print("c" in d)
74. Output?
A. True
B. False d = {1:10}
print([Link](2, 50))
C. Error
D. None
A. 2
Answer: B
B. 10
C. 50
D. Error
Answer: C
71. Most common dictionary loop?
A. for x in d:
B. for x,y in d:
75. Which creates dictionary from
C. for x in [Link]():
D. for x in [Link](): keys?
Answer: A
A. fromkeys()
B. make()
C. create()
D. keysdict()
Answer: A
Mahesh Srivastava 9839263360 Page 18
76. Output? d = {1:10,2:20}
print(min([Link]()))
print([Link]([1,2,3], 0))
A. 10
A. Error B. 20
B. {1:2:3} C. 1
C. {1:0, 2:0, 3:0} D. Error
D. None Answer: A
Answer: C
81. Output?
77. Output?
d={"a":10}
print("a" not in d)
d = {1:2, 3:4}
print(d[3])
A. True
A. 3 B. False
B. 4 C. Error
C. Error D. None
D. None Answer: B
Answer: B
82. Which gets both key & value?
78. To convert dictionary to list of
A. pair()
tuples:
B. items()
C. getall()
A. toList()
D. list()
B. items()
C. makeList() Answer: B
D. tuples()
Answer: B
83. Output?
d={"a":1,"b":2}
79. Output? print(len([Link]()))
d = {"x":1}
A. 1
print(type([Link]()))
B. 2
C. 0
A. list
D. Error
B. dict_values
C. tuple Answer: B
D. set
Answer: B
84. Output?
d={1:2, 3:4}
80. Output? print([Link](3))
Mahesh Srivastava 9839263360 Page 19
A. 4 A. {1:10}
B. 3 B. {}
C. None C. Error
D. Error D. None
Answer: A Answer: B
85. Which creates shallow copy? 89. Dictionary stores data in:
A. clone() A. Key-value pair
B. copy() B. Single values
C. new() C. Only keys
D. duplicate() D. Only values
Answer: B Answer: A
86. Output? 90. Output?
d={"x":1} d={1:10, 2:20}
d["x"]=5 print(3 in d)
print(d["x"])
A. True
A. 1 B. False
B. 5 C. None
C. Error D. Error
D. None Answer: B
Answer: B
91. Dictionary values can be:
87. Output?
A. same
d={1:2,2:3,3:4} B. different
print(sorted(d))
C. both A & B
D. none
A. [1,2,3]
Answer: C
B. [2,3,4]
C. Error
D. None
Answer: A
92. Output?
d={"a":1,"b":2}
print([Link]("c"))
88. Output?
A. None
d={1:10} B. c
[Link]()
print(d) C. Error
Mahesh Srivastava 9839263360 Page 20
D. 1
Answer: A
97. Output?
d={1:10,2:20}
93. Output? print(20 in [Link]())
d = {"x":10} A. True
print([Link]()) B. False
C. Error
A. ["x"] D. None
B. dict_keys(['x']) Answer: B
C. x
D. ('x')
Answer: B
98. Output?
d={1:"A",2:"B"}
94. Dictionary keys are: print(type(d))
A. Unique A. dict
B. Duplicate B. list
C. Anonymous C. tuple
D. Deleted D. set
Answer: A Answer: A
95. Output? 99. Output?
d={"a":1,"b":2} d={1:2,3:4}
print("A" in d) print([Link]())
A. True A. (1,2)
B. False B. (3,4)
C. Error C. Error
D. None D. None
Answer: B Answer: B
96. Which method converts 100. What is a key feature of
dictionary to list of values? dictionaries?
A. values() A. Index-based
B. listvalues() B. Ordered collection
C. getvalues() C. Key-value mapping
D. tolist() D. Duplicate keys
Answer: A Answer: c
Mahesh Srivastava 9839263360 Page 21
Mahesh Srivastava 9839263360 Page 22