Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to update and remove elements from a Python dictionary

Wale Moshood

A dictionary in Python is a collection that is ordered, changeable, and does not allow duplicate keys. It is used to store data values in key-value pairs. Dictionaries are written with curly brackets. An example is given below:

sample_dict = {
  "type": "Writing",
  "language": "English",
  "year": 2021

Dictionary operations

Update a dictionary

Python allows a dictionary object to be mutable, meaning update or add operations are permissible. A new item can be pushed or an existing item can be modified with the aid of an assignment operator.

If an element is added to a key that already exists, its value will be changed to the newly added value. Upon adding a fresh key-value pair, a new item will be added to the existing dictionary.

The different methods to update a dictionary are:

  • The assignment method
  • The update method

The example below demonstrates both the addition and update operations on the existing dictionary object.

my_dict = {'Name': 'Henry', 'Age': 16, 'Subject': 'English'}
print("The student who left is", my_dict.get('Name'))

my_dict['Name'] = 'Perry'
print("The student who replaced him is", my_dict.get('Name'))

my_dict['Name'] = 'Smith'
print("The student who joined is", my_dict.get('Name'))
  • The update method is an alternative method to modify the value of an existing item.
sample_dict = {"java" : 1, "python" : 2, "nodejs" : 3}
sample_dict.update( nodejs = 2 )
sample_dict.update( python = 3 )

Remove items

Several methods are provided by Python to remove items/elements from a dictionary. They include:

The pop() method:

  • This is the most common method to remove items from a dictionary. pop() takes a key as an input and deletes the corresponding item/element from the Python dictionary. It returns the value associated with the input key.

The popitem() method:

  • This method removes and returns a random item key-value from the dictionary.

The clear() method:

  • This method drops all the items from the dictionary. clear() is referred to as the flush method because it flushes everything from the dictionary.

The del method:

  • Another way to remove an element from a dictionary is to use the del keyword. del deletes individual elements and eventually the entire dictionary object.
food_items = {1:"rice", 2:"beans", 3:"yam", 4:"plantain", 5:"potatoes", 6:"wheat"}

# Delete a specific element

# Delete a random element

# Remove a specific element
del food_items[4]

# Delete all elements from the dictionary

# Eliminates the whole dictionary object
del food_items

The clear() method above removes all the elements from the Python dictionary, leaving it empty. Hence, the subsequent call for the del method on the dictionary object removes it altogether. So, adding print(food_items) after del food_items fails and returns a NameError.



View all Courses

Keep Exploring