Does the dictionary pop() function remove the item from the dictionary?


#1

Question

Does the pop() function on a dictionary just access the data or remove it?

Answer

The pop() function will return the value for the key AND then remove the key/value from the dictionary. The following code example shows the results of performing a pop() on a dictionary to remove the item indexed by key “C”.

letters = { "A": 10, "B": 20, "C": 30, "D": 40 }

value = letters.pop("C")

print(letters)
# {'A': 10, 'B': 20, 'D': 40}