dict.keys()
dict.keys() dict_keys · Updated March 13, 2026 · Dict Methods The .keys() method returns a view object that displays a list of all keys in a dictionary. This view provides a dynamic view of the dictionary’s keys, meaning it reflects any changes made to the dictionary.
Syntax
dict.keys()
Parameters
None. This method takes no parameters.
Return Value
Returns a dict_keys view object, which yields dictionary keys on iteration. The view is dynamic—if the dictionary changes, the view reflects those changes.
Basic Examples
Getting all keys from a dictionary
person = {"name": "Alice", "age": 30, "city": "New York"}
print(person.keys())
# dict_keys(['name', 'age', 'city'])
Iterating over keys
The most common use is iterating over dictionary keys:
config = {"host": "localhost", "port": 8080, "debug": True}
for key in config.keys():
print(key)
# host
# port
# debug
Converting keys to a list
If you need an actual list instead of a view:
fruits = {"apple": 1, "banana": 2, "cherry": 3}
keys_list = list(fruits.keys())
print(keys_list)
# ['apple', 'banana', 'cherry']
How Views Work
A key distinction: .keys() returns a view, not a copy. Changes to the dictionary are reflected in the view:
d = {"a": 1, "b": 2}
keys_view = d.keys()
# Add a new key to the dictionary
d["c"] = 3
# The view now shows the new key!
print(list(keys_view))
# ['a', 'b', 'c']
If you need a snapshot that won’t change, convert to a list first:
d = {"a": 1}
snapshot = list(d.keys())
d["b"] = 2
# Snapshot unchanged
print(snapshot)
# ['a']
Common Patterns
Checking if a key exists
user = {"username": "alice", "email": "alice@example.com"}
if "email" in user.keys():
print("Email is present")
# Email is present
# The 'in' operator works directly on dict too:
if "phone" not in user:
print("No phone number")
# No phone number
Using keys() with other dict methods
data = {"x": 10, "y": 20, "z": 30}
# Get keys and values together
keys = list(data.keys())
values = list(data.values())
print(keys)
# ['x', 'y', 'z']
print(values)
# [10, 20, 30]
Dictionary comprehension with keys
original = {"a": 1, "b": 2, "c": 3}
# Create new dict with modified keys (if needed)
# Note: keys are immutable, so this creates a new dict
doubled = {k: v * 2 for k, v in original.items()}
print(doubled)
# {'a': 2, 'b': 4, 'c': 6}
Iterating with enumeration
inventory = {"apples": 5, "bananas": 3, "oranges": 8}
for i, key in enumerate(inventory.keys()):
print(f"{i + 1}. {key}")
# 1. apples
# 2. bananas
# 3. oranges
Using zip to combine keys with values
keys = ["name", "age", "city"]
values = ["Alice", 30, "New York"]
# Combine into a dictionary
data = dict(zip(keys, values))
print(data)
# {'name': 'Alice', 'age': 30, 'city': 'New York'}
keys() vs items() vs values()
| Method | Returns | Example |
|---|---|---|
.keys() | View of keys | ['name', 'age'] |
.values() | View of values | ['Alice', 30] |
.items() | View of (key, value) tuples | [('name', 'Alice'), ('age', 30)] |
Performance Note
The .keys() view is memory-efficient because it doesn’t create a copy of the data. For most operations, you don’t even need to call .keys() explicitly—the dictionary itself is iterable over keys:
d = {"a": 1, "b": 2, "c": 3}
# Both are equivalent:
for key in d.keys():
print(key)
for key in d:
print(key)
See Also
-
dict() — the dict constructor for creating new dictionaries
-
dict.items() — returns key-value pairs as tuples
-
collections module — contains defaultdict and Counter for specialized dictionary operations