Python dictionary keys() method
Return the keys.
Syntax
dict.keys()
Looping over the dictionary itself gives the keys too, so .keys() is mostly used to make a list or to compare key sets.
Parameters
None.
Return value
a view of the keys
Example
Code
prices = {"tea": 3.0, "coffee": 4.5} print(list(prices.keys())) print("tea" in prices) # no need for .keys() here other = {"coffee": 4.0, "cake": 5.0} print(prices.keys() & other.keys()) # keys in both
Output
['tea', 'coffee']
True
{'coffee'}
Related
Learn it step by step in the free lesson Dictionaries & Sets, with exercises that check your answers.