PYTHON DICTIONARY
Methods to Delete Elements from a Dictionary
Method / Statement Purpose Example Notes
Deletes a
Raises error if
del dict[key] specific key- del d["name"]
key doesn't exist
value pair
Raises error if
Removes and
key not found
[Link](key) returns value [Link]("marks")
(unless default
of the key
provided)
Safe version of
No error if key
[Link](key,default) pop() with [Link]("age","NotFound")
not found
fallback
Removes and Error if
[Link]() returns last [Link]() dictionary is
inserted item empty
Deletes all Dictionary
[Link]() [Link]()
items becomes empty
Use list() to
Deletes all for k in list(d): avoid runtime
Loop with del
items manually del d[k] error during
iteration
Methods to Delete Elements from a Dictionary
1. Using del statement
Deletes a specific key-value pair.
student = {"name": "Aman", "class": 12, "marks": 92}
del student["class"]
print(student) # {'name': 'Aman', 'marks': 92}
Error if the key does not exist.
2. Using pop(key) method
Removes the key-value pair and returns the value of the key.
marks = [Link]("marks")
print(marks) # 92
print(student) # {'name': 'Aman'}
Error if the key does not exist unless a default is provided.
[Link]("age", "Key not found") # Safe version
3. Using popitem() method
Removes and returns the last inserted key-value pair (since Python 3.7+).
student = {"name": "Aman", "class": 12}
item = [Link]()
print(item) # ('class', 12)
print(student) # {'name': 'Aman'}
Error if dictionary is empty.
4. Using clear() method
Removes all key-value pairs from the dictionary.
[Link]()
print(student) # {}
Bonus: Deleting all keys manually in a loop (advanced)
for key in list([Link]()):
del student[key]
Basic Dictionary Operations
Operation Description Example
Returns the number of key-
len(d) len(student) → 3
value pairs
Operation Description Example
d[key] Access the value using the key student["name"] → 'Aman'
d[key] = value Adds or updates a key-value pair student["age"] = 17
del d[key] Deletes the key-value pair del student["marks"]
key in d Checks if key exists "class" in student → True
Common Dictionary Functions/Methods
Function / Method Description Example
Returns value of
key; returns
[Link](key[, default]) [Link]("age", 0)
default if key
not found
[Link]() Returns all keys list([Link]())
[Link]() Returns all values list([Link]())
Returns all key-
[Link]() value pairs as list([Link]())
tuples
Adds key-value
pairs from
[Link](other_dict) [Link]({"school":"ABC"})
another
dictionary
Removes and
[Link](key[, default]) returns the value [Link]("class")
of the key
[Link]() [Link]()
Removes all
Function / Method Description Example
items
Returns a shallow
[Link]() new_dict = [Link]()
copy
Dictionary Traversal
Emp={'name':'ram','sal':20000,'age':23}
for x in d:
print(x,’:’,d[x])
Emp={'name':'ram','sal':20000,'age':23}
for x in [Link]():
print(x)
for x,y in [Link]():
print(x,y)
for x in [Link]():
print(x)
Output:
name:ram
sal:20000
age:23
('name', 'ram')
('sal', 20000)
('age', 23)
name ram
sal 20000
age 23
ram
20000
23
Dictionary Examples
# Creating a dictionary
student = {"name": "Aman", "class": 12, "marks": 92}
# Adding a key
student["age"] = 17
# Updating a value
student["marks"] = 95
# Accessing keys and values
for k, v in [Link]():
print(k, "->", v)
Worksheet 01: Deleting Dictionary Elements
Q1. What will be the output of the following code?
d = {"a": 1, "b": 2, "c": 3}
del d["b"]
print(d)
Q2. Predict the output:
d = {"name": "Ankit", "marks": 90}
x = [Link]("marks")
print(x)
print(d)
Q3. What will happen if we run this?
d = {"x": 100}
del d["y"]
a) Deletes key 'y'
b) Returns None
c) Error
d) Deletes all elements
Q4. Fill in the blank to remove the last inserted element:
data = {"id": 1, "class": 12}
removed_item = _______________
print(removed_item)
Q5. Write a program to delete all elements from a dictionary using a
loop.
Worksheet 02: Python Dictionary
Section A: Fill in the Blanks
1. A dictionary in Python is a collection of __________ pairs.
2. The method [Link](key) returns __________ if the key is not
present in the dictionary.
3. Dictionaries are enclosed in __________ brackets.
4. The method to remove all items from a dictionary is __________.
5. [Link]() returns a __________ of all keys in the dictionary.
Section B: State True or False
1. Dictionary keys can be duplicated.
2. The pop() method removes a key-value pair by key.
3. Dictionaries are ordered in Python 3.7 and later.
4. You can use integers, strings, or tuples as dictionary keys.
5. [Link]() returns a list of values in the dictionary.
Section C: Short Answer Questions
Q.1 What is the difference between pop() and popitem() methods?
Q.2 Write a Python statement to check if the key "name" exists in
dictionary student.
Q.3 How can you update the value of a key "marks" to 95 in a
dictionary d?
Q.4 Can a list be used as a dictionary key? Justify your answer.
Q.5 What is the use of the items() method?
Section D: Output-Based Questions
Q1. Predict the output:
d = {"a": 10, "b": 20}
print([Link]("c", 0))
print(d["b"])
Q2. Predict the output:
d = {1: "one", 2: "two"}
d[3] = "three"
d[2] = "TWO"
print(d)
Q3. Predict the output:
d = {"x": 100, "y": 200, "z": 300}
print([Link]())
print(d)
Section E: Programming Questions
Q1. Write a Python program to create a dictionary from user input
containing names as keys and marks as values. Then print all
students who scored more than 75.
Q2. Write a Python function that accepts a dictionary and returns the
key with the maximum value.
Q3. Given a dictionary of employee data:
employees = {
"emp1": {"name": "John", "salary": 50000},
"emp2": {"name": "Jane", "salary": 60000},
"emp3": {"name": "Doe", "salary": 55000}
Write code to increase the salary of each employee by 10%.