Dict Pop in Python

Level 3112 keystrokes

This removes and returns a value from a dictionary using the pop() method with a key.

Python code

# Create a dictionary with three key-value pairs
1data = {"a": 1, "b": 2, "c": 3}
# pop(key) removes the key and returns its value
# The key "b" is removed, value 2 is returned
2removed = data.pop("b")
# Print the removed value
3print(f"Removed: {removed}")
# Print remaining dictionary (without "b")
4print(f"Remaining: {data}")

Output

Removed: 2
Remaining: {'a': 1, 'c': 3}
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • "× 12
  • :× 5
  • (× 3
  • )× 3
  • {× 3
  • }× 3
  • ,× 2
  • =× 2
  • .× 1

More Python examples at level 3

← Python typing practice