Python 3 reference · Dictionary Methods

Python Dictionary keys() Method

Return a live view of the dictionary's keys. Supports len(), in and set operations such as & and |.

Python 3 Since 3.0

Syntax

dict.keys()

Return value

A dict_keys view.

Examples

List and test keysTry it yourself
person = {"name": "Ada", "year": 1815}
print(list(person.keys()))
print("name" in person)  # no need for .keys()

Output

['name', 'year']
True
Keys support set operationsTry it yourself
a = {"x": 1, "y": 2}
b = {"y": 3, "z": 4}
print(a.keys() & b.keys(), a.keys() - b.keys())

Output

{'y'} {'x'}

Version history

VersionChange
Python 3.0Added