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
person = {"name": "Ada", "year": 1815}
print(list(person.keys()))
print("name" in person) # no need for .keys()
Output
['name', 'year'] True
a = {"x": 1, "y": 2}
b = {"y": 3, "z": 4}
print(a.keys() & b.keys(), a.keys() - b.keys())
Output
{'y'} {'x'}Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
dict.values()· Return a live view of the dictionary's values, for looping or for functions such as sum() and max().dict.items()· Return a live view of the dictionary's (key, value) pairs, used to loop over keys and values together.- Python Dictionaries · tutorial