Dictionary Items in Python

Level 255 keystrokes

This iterates over key-value pairs using the items() method of a dictionary.

Python code

# Create a dictionary with two key-value pairs
1d = {"a": 1, "b": 2}
# items() returns key-value pairs as tuples
# k gets the key, v gets the value in each iteration
2for k, v in d.items():
# Print each key and its corresponding value
3 print(k, v)

Output

a 1
b 2
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.

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

More Python examples at level 2

← Python typing practice