How to Loop over a Dictionary's Keys in Python with for
A for loop over a Python dictionary gives you its keys, one at a time, in the order they were added. With a key in hand, the value is one lookup away. This guide loops over a small price list on Python 3.15.2, first for the keys alone and then for each key with its value.
Syntax
for <key> in <dictionary>:
<code that uses key and dictionary[key]>
| Part | What it means |
|---|---|
<key> | A name you choose. Each time round the loop it holds the next key |
<dictionary> | The dictionary to go through |
<dictionary>[<key>] | The value stored under that key |
The built-in types reference says what a loop over a dictionary uses: iter(d) "Return an iterator over the keys of the dictionary. This is a shortcut for iter(d.keys())." The same page adds: "Dictionaries preserve insertion order."
The Example
A dictionary prices maps three shop items to their prices. The first loop prints each item's name. The second prints each name with its price.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
prices = {"apple": 0.5, "bread": 2.5, "milk": 1.2}
for name in prices:
print(name)
for name in prices:
print(name, "costs", prices[name])
![The prices.py script in a macOS-style code window: a prices dictionary with apple 0.5, bread 2.5 and milk 1.2, a loop that prints each name, and a loop that prints each name, the word costs, and prices[name].](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-dict-keys-loop-01-code.webp)
for name in prices: runs the indented line once for each key, with name holding that key. In the second loop, prices[name] looks up the value stored under the current key.
Step 2: Run It
uv run python prices.py
apple
bread
milk
apple costs 0.5
bread costs 2.5
milk costs 1.2

The first loop printed the three keys, in the order they were written in the dictionary. The second printed each key with the value it looked up. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting the loop to give the values | for price in prices: total += price adds the keys, so it stopped at the first one: TypeError: unsupported operand type(s) for +=: 'int' and 'str' | Loop over the keys and add prices[name]: the total came out as 4.2 |
FAQ
Is for name in prices.keys(): any different?
No. Both loop over the same keys in the same order. A for loop gets its items from iter(d), which the documentation calls "a shortcut for iter(d.keys())".
How do I get each key and its value without a lookup?
Loop over prices.items(), which gives each key with its value as a pair. The Python tutorial says: "When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method."
Conclusion
A plain for loop over prices gave its keys in the order they were added, and prices[name] turned each key into its price. Expecting values from that loop is the usual slip, and it ends in a TypeError.
Additional Resources
- Mapping types: dict — everything a dictionary can do.
- Looping techniques — loops over dictionaries and other collections.
