Dictionary Methods in Python
In this exercise, you will learn about the various dictionary methods in Python.
Removing items from dictionary There are a few methods that we can use to remove items from dictionary.
a) clear() method The clear() method removes all the items from the dictionary.
Example:
Python
# Creating a dictionary
student = {
"name": "Ashish", # key = "name", value = "Ashish"
"age": 29, # key = "age", value = 29
"course": "Python", # key = "course", value = "Python"
"marks": [85, 90, 92], # value can be a list too
"eligible": True # value can be a boolean
}
# clear items from dictionary
student.clear()
print(student) # output: {} b) pop() method The pop() method removes the key-value pair whose key is passed as a parameter.
Example:
Python
# Creating a dictionary
student = {
"name": "Ashish", # key = "name", value = "Ashish"
"age": 29, # key = "age", value = 29
"course": "Python", # key = "course", value = "Python"
"marks": [85, 90, 92], # value can be a list too
"eligible": True # value can be a boolean
}
# Remove specific key-value pair and return the value
print(student.pop('course'))
# output: Python
print(student)
# output: {'name': 'Ashish', 'age': 29, 'marks': [85, 90, 92], 'eligible': True} c) popitem() method The popitem() method removes the last key-value pair from the dictionary and returns that key-value pair in the form (key, value).
Raises KeyError if the dictionary is empty.
Example:
Python
# Creating a dictionary
student = {
"name": "Ashish", # key = "name", value = "Ashish"
"age": 29, # key = "age", value = 29
"course": "Python", # key = "course", value = "Python"
"marks": [85, 90, 92], # value can be a list too
"eligible": True # value can be a boolean
}
# Remove last key-value pair and return the value
print(student.popitem())
# output: ('eligible', True)
print(student)
# output: {'name': 'Ashish', 'age': 29, 'course': 'Python', 'marks': [85, 90, 92]} d) del keyword we can also use the del keyword to remove a dictionary item. If key is not provided, then the del keyword will delete the dictionary entirely.
Example:
Python
# Creating a dictionary
student = {
"name": "Ashish", # key = "name", value = "Ashish"
"age": 29, # key = "age", value = 29
"course": "Python", # key = "course", value = "Python"
"marks": [85, 90, 92], # value can be a list too
"eligible": True # value can be a boolean
}
# Remove a specific key-value pair and return the value
del student["age"]
print(student) # {'name': 'Ashish', 'course': 'Python', 'marks': [85, 90, 92], 'eligible': True}
# Delete the entire dictionary
del student
print(student)
# output: NameError: name 'student' is not defined Length of the Dictionary The len() method is used to return the length (the number of items) in the dictionary.
Example:
Python
# Creating a dictionary
student = {
"name": "Ashish", # key = "name", value = "Ashish"
"age": 29, # key = "age", value = 29
"course": "Python", # key = "course", value = "Python"
"marks": [85, 90, 92], # value can be a list too
"eligible": True # value can be a boolean
}
# Get the number of key-value pairs in the dictionary
count = len(student)
print(count) # 5 Copy Dictionary The copy() method returns a shallow copy of the dictionary.
Example:
Python
# Creating a dictionary
student = {
"name": "Ashish", # key = "name", value = "Ashish"
"age": 29, # key = "age", value = 29
"course": "Python", # key = "course", value = "Python"
}
new_student = student.copy()
student.update({"name": "Ashish Kumar", "age": 30, "phone": 1234567890})
print(student)
# {'name': 'Ashish Kumar', 'age': 30, 'course': 'Python', 'phone': 1234567890}
print(new_student)
# {'name': 'Ashish', 'age': 29, 'course': 'Python'} Update Dictionary Items As Dictionary is mutable, so we can add or update the item in it. The update() method updates the value of the key provided to it if the item already exists in the dictionary, else it creates a new key-value pair.
Example:
Python
# Creating a dictionary
student = {
"name": "Ashish", # key = "name", value = "Ashish"
"age": 29, # key = "age", value = 29
"course": "Python", # key = "course", value = "Python"
}
student.update({"name": "Ashish Kumar", "age": 30, "phone": 1234567890})
print(student)
# {'name': 'Ashish Kumar', 'age': 30, 'course': 'Python', 'phone': 1234567890}
# Another way
# Changing elements in a Dictionary
student['name'] = 'Katrina Kaif' # update value
student['age'] = 35 # update value
student['address'] = 'Mumbai' # add new entry
print(student)
# {'name': 'Katrina Kaif', 'age': 35, 'course': 'Python', 'phone': 1234567890, 'address': 'Mumbai'} items() method of Dictionary The items() method returns a new object of the dictionary's items in (key, value) format.
Example:
Python
student = {"name": "Ashish", "age": 29, "course": "Python"}
print(student.items())
# dict_items([('name', 'Ashish'), ('age', 29), ('course', 'Python')])