Python dict.keys() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
dict method

Definition

keys() returns a view of the dict's keys, printed as dict_keys([...]). A view is not a copy: it is a window onto the dict, so it always shows the current keys in insertion order.

Because keys are unique and hashable, the view is set-like: it supports -, &, |, and ^ with sets and other key views. That makes it ideal for comparing which fields two records share.

dict.keys() is an instance method of the built-in dict type: call it on a dictionary with dot notation, such as menu.keys(...). No import is needed.

Syntax

Call it with no arguments. Most code loops over the view or converts it with list().

python
view = d.keys()
for key in d.keys():
    print(key)
CallWhat it does
{"a": 1, "b": 2}.keys()Returns dict_keys(['a', 'b']).
list({"x": 0, "y": 0}.keys())Returns ['x', 'y'].
{"a": 1, "b": 2}.keys() - {"a"}Returns {'b'}, a regular set.

key in d and key in d.keys() give the same answer with the same fast lookup; the shorter form is usual. list(d) equals list(d.keys()).

Method signatures
python
dict.keys()
# returns: a dict_keys view of the dict's keys

Parameters

keys() takes no parameters.

Return Value

Return ValueDescription
dict_keysA live, read-only view of the keys in insertion order. It supports len(), in, iteration, reversed() (Python 3.8+), and set operators.

Python Examples

Example 1 — Inspect the Keys of a Cafe Menu

Print a keys view, test membership, convert it to a list, and see what happens when you try to index it.

python
menu = {"latte": 4.5, "mocha": 5.0, "tea": 3.0}
names = menu.keys()

print(names)
print("mocha" in names, "chai" in menu)
print(list(names), len(names))
try:
    print(names[0])
except TypeError:
    print("views cannot be indexed; use list(names)[0]")

How It Works

  1. Readable repr. Printing the view shows dict_keys(...) wrapped around the keys in menu order.
  2. Two ways to test. "mocha" in names and "chai" in menu both check keys, never prices.
  3. Convert when needed. list(names) makes a real list, while len() works on the view directly.
  4. No positions. Indexing the view raises TypeError; a view supports lookups by key, not by position.

Example 2 — Watch a Keys View Follow a Server Registry

Create the view once, then add and remove servers and print the same view again.

python
servers = {"web-1": "up", "db-1": "up", "mail-1": "down"}
names = servers.keys()
print(len(names))

servers["api-1"] = "starting"
del servers["web-1"]
print(names)
print(list(reversed(names)))
print(sorted(names))

How It Works

  1. Snapshot-free. names was created while there were three servers, and it reports 3.
  2. Changes show up. After adding api-1 and deleting web-1, the very same view lists the new keys; nothing was re-fetched.
  3. Newest first. reversed() on a keys view (Python 3.8+) walks from the last inserted key back to the first.
  4. Alphabetical. sorted() returns a new sorted list and leaves the dict order untouched.

Example 3 — Validate Form Fields with Set Operations

Find required fields that are missing, unexpected extra fields, and the fields both sides share.

python
required = {"name", "email", "age"}
form = {"name": "Kofi", "email": "[email protected]", "city": "Accra"}

print(sorted(required - form.keys()))
print(sorted(form.keys() - required))
print(sorted(form.keys() & required))
print(form.keys().isdisjoint({"phone", "fax"}))

How It Works

  1. Missing fields. Set minus keys view leaves the required names the form does not have: only age.
  2. Extra fields. Reversing the subtraction gives keys that are not required, here city.
  3. Shared fields. & keeps names on both sides. The results are ordinary sets, so sorted() prints them in a stable order.
  4. No overlap check. isdisjoint() returns True because the form has neither a phone nor a fax field.

Frequently Asked Questions

A dict_keys view: a live, read-only, set-like window onto the keys in insertion order. Wrap it in list() when you need a real list.
No, both use the same fast hash lookup. key in d is shorter and is the usual style.
Use list(d) or list(d.keys()). For alphabetical order use sorted(d).
A view is not a sequence and has no positions. Use list(d)[0], or next(iter(d)) to get the first key without building a list.
Use a.keys() & b.keys(). It returns a set; a.keys() - b.keys() gives keys only in a.

Next: dict.pop()

Remove a key and return its value, with an optional default for missing keys.

pop() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful