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
dict.pop(key) returns value d.pop("marks")
(unless default
of the key
provided)
Safe version of
No error if key
dict.pop(key,default) pop() with d.pop("age","NotFound")
not found
fallback
Removes and Error if
dict.popitem() returns last d.popitem() dictionary is
inserted item empty
Deletes all Dictionary
dict.clear() d.clear()
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 = student.pop("marks")
print(marks) # 92
print(student) # {'name': 'Aman'}
Error if the key does not exist unless a default is provided.
student.pop("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 = student.popitem()
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.
student.clear()
print(student) # {}
Bonus: Deleting all keys manually in a loop (advanced)
for key in list(student.keys()):
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
d.get(key[, default]) student.get("age", 0)
default if key
not found
d.keys() Returns all keys list(student.keys())
d.values() Returns all values list(student.values())
Returns all key-
d.items() value pairs as list(student.items())
tuples
Adds key-value
pairs from
d.update(other_dict) student.update({"school":"ABC"})
another
dictionary
Removes and
d.pop(key[, default]) returns the value student.pop("class")
of the key
d.clear() student.clear()
Removes all
Function / Method Description Example
items
Returns a shallow
d.copy() new_dict = student.copy()
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 Emp.items():
print(x)
for x,y in Emp.items():
print(x,y)
for x in Emp.values():
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 student.items():
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 = d.pop("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 dict.get(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. dict.keys() 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. dict.values() 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(d.get("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(d.popitem())
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%.