items()

noun

say it: “EYE-temz” /ˈaɪtəmz/

A dictionary method that hands back every key with its value as pairs, so a loop can use both at once.

What does items() do?

items() is a dictionary method that gives back each key together with its value, as pairs. In a for loop you can name both halves at once.

stock = {"apples": 12, "pears": 0}
for fruit, count in stock.items():
    print(f"{fruit}: {count}")
# apples: 12
# pears: 0

In a sentence

  • “Using items() saved her from looking up every value by its key.”
  • “The loop over items() printed each player's name beside their score.”

Level: Apprentice · More Python terms at this level

Updated