14.4 Remove Dictionary Items

By | October 4, 2021

Removing Items

There are several methods to remove items from a dictionary:

Example

The pop() method removes the item with the specified key name:
thisdict = {
  “brand”: “Ford”,
  “model”: “Mustang”,
  “year”: 1964
}
thisdict.pop(“model”)
print(thisdict)

Output:
{‘brand’: ‘Ford’, ‘year’: 1964}

Example

The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):
thisdict = {
  “brand”: “Ford”,
  “model”: “Mustang”,
  “year”: 1964
}
thisdict.popitem()
print(thisdict)

Output:
{‘brand’: ‘Ford’, ‘model’: ‘Mustang’}

Example

The del keyword removes the item with the specified key name:
thisdict = {
  “brand”: “Ford”,
  “model”: “Mustang”,
  “year”: 1964
}
del thisdict[“model”]
print(thisdict)

Output:
{‘brand’: ‘Ford’, ‘year’: 1964}

Example

The del keyword can also delete the dictionary completely:
thisdict = {
  “brand”: “Ford”,
  “model”: “Mustang”,
  “year”: 1964
}
del thisdict
print(thisdict) #this will cause an error because “thisdict” no longer exists.

Output:
Traceback (most recent call last):
  File “demo_dictionary_del3.py”, line 7, in <module>
    print(thisdict) #this will cause an error because “thisdict” no longer exists.
NameError: name ‘thisdict’ is not defined

Example

The clear() method empties the dictionary:
thisdict = {
  “brand”: “Ford”,
  “model”: “Mustang”,
  “year”: 1964
}
thisdict.clear()
print(thisdict)

Output:
{ }

Leave a Reply

Your email address will not be published. Required fields are marked *