Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober 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 PC×
Skip to content
World desk2 min

How to Get Keys of a Dictionary in Python

Use dict.keys() for a live key view, list(my_dict) for a separate list, or for key in my_dict to process keys directly.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use my_dict.keys() to get a live view of a dictionary’s keys. If you need a separate list, use list(my_dict) or list(my_dict.keys()). To process keys in a loop, iterate over the dictionary directly with for key in my_dict:.

Get a keys view with dict.keys()

The keys() method returns a view connected to the dictionary, not a new list. The view reflects changes made to the dictionary:

person = {"name": "Ada", "language": "Python"}
keys_view = person.keys()

print(keys_view)  # dict_keys(['name', 'language'])

Use this form when you want a live view or need to check whether a key is present. For example, "name" in person.keys() checks membership; "name" in person is the shorter equivalent.

The behavior of dictionary key views is documented in the Python 3.14.8 built-in types reference.

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

Get the keys as a list

Use list(my_dict) for a separate list of keys. It is equivalent to list(my_dict.keys()):

person = {"name": "Ada", "language": "Python"}
keys_list = list(person)

print(keys_list)  # ['name', 'language']

The list is a snapshot: later changes to the dictionary do not update it. That makes it useful when you need a list-shaped collection or want to change the dictionary while processing the original keys.

Loop over dictionary keys

A dictionary yields its keys when iterated, so you do not need to call keys() in a loop:

for key in person:
    print(key)

This is equivalent to for key in person.keys():. Use direct iteration when you simply want to handle each key.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Key order and changing a dictionary during iteration

Dictionaries preserve insertion order; this became a language guarantee in Python 3.7. As a result, both direct iteration and list(my_dict) yield keys in that order. The official reference also documents reverse iteration over dictionaries and keys views starting in Python 3.8.

Avoid adding or deleting dictionary entries while iterating over its keys view. Such changes may raise RuntimeError or cause the iteration to miss entries. If you need to modify the dictionary as you process keys, iterate over a list snapshot instead:

for key in list(person):
    if key == "language":
        del person[key]

These ordering, reversal, and iteration details are specified in the official Python built-in types reference.

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.

Free tools Windows power users keep installed

One-click scans. No signup required.

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 *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver 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.