October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Loop Through a Python Dictionary: Keys, Values, and Items

Choose a Python dictionary loop by what you need: keys, values, key/value pairs, or keys in sorted order. See how insertion order works and what to avoid changing during iteration.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use for key in d to visit dictionary keys, for value in d.values() to visit values, and for key, value in d.items() when you need both. A dictionary’s iteration order is insertion order in Python 3.7 and later.

Choose a loop based on what you need

For example, start with a dictionary mapping names to scores:

As an Amazon Associate I earn from qualifying purchases.

scores = {"Ada": 95, "Lin": 88}
Loop form What it yields Use it when
for key in d Keys You need keys, or want to look up each value with d[key].
for value in d.values() Values You do not need the keys.
for key, value in d.items() Key/value pairs, unpacked into two variables You need each key alongside its value.
for key in sorted(d) Keys in sorted order You want sorted traversal instead of insertion order.

Loop over keys

A dictionary is directly iterable, and a plain loop visits its keys. Use the key to retrieve the corresponding value when needed:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
for name in scores:
    print(name, scores[name])

This is equivalent to looping over scores.keys() when you specifically want to make the keys view explicit.

Loop over values

Call values() when the keys are irrelevant:

for score in scores.values():
    print(score)

Loop over keys and values together

Call items() to get each entry as a (key, value) pair. Python can unpack each pair directly into loop variables:

for name, score in scores.items():
    print(name, score)

What order does dictionary iteration use?

Python guarantees insertion order for dictionary iteration starting with Python 3.7. The Python 3.6 CPython implementation also preserved insertion order, but that behavior was an implementation detail rather than a language guarantee.

Changing the value of an existing key leaves that key in its original position. If you delete a key and later add it again, it appears at the end of the iteration order.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to iterate over keys in sorted order

Pass the dictionary to sorted() to iterate through its keys in sorted order:

for key in sorted(scores):
    print(key, scores[key])

sorted(scores) returns a sorted list of keys; it does not rearrange the dictionary or change its insertion order. Sorting requires the keys to be comparable with one another.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Can you change a dictionary while looping over it?

The keys(), values(), and items() methods return dynamic views that reflect changes to the underlying dictionary. Avoid adding or deleting entries while iterating over one of these views: Python’s documentation warns that doing so may raise a RuntimeError or cause the iteration to miss entries. If you need to change which entries exist, iterate over a separate list of keys, such as list(d), and make changes to the dictionary in that loop.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.