Python‎ > ‎dict‎ > ‎

dict.keys()


Introduction
The display the keys of the dictionary.

Syntax
dict.keys()

Parameters
None

Examples
The headers returns with the server response to get request and if statement is used to open the webbrowser condition to dict.keys.
 >>>import requests
>>> re=requests.get('http://www.pythonlake.com', params='Pythonlake')
>>> re.headers
{'x-content-type-options': 'nosniff', 'server': 'GSE', 'x-robots-tag': 'noarchive', 'x-xss-protection': '1; mode=block', 'date': 'Mon, 27 Apr 2015 13:27:21 GMT', 'accept-ranges': 'none', 'vary': 'Accept-Encoding', 'cache-control': 'public, max-age=5', 'transfer-encoding': 'chunked', 'content-type': 'text/html; charset=utf-8', 'last-modified': 'Mon, 27 Apr 2015 13:16:45 GMT', 'expires': 'Mon, 27 Apr 2015 13:27:26 GMT', 'alternate-protocol': '80:quic,p=1,80:quic,p=1'}
>>> 
>>> h.keys
<bound method CaseInsensitiveDict.keys of {'x-content-type-options': 'nosniff', 'server': 'GSE', 'x-robots-tag': 'noarchive', 'x-xss-protection': '1; mode=block', 'date': 'Mon, 27 Apr 2015 13:27:21 GMT', 'accept-ranges': 'none', 'vary': 'Accept-Encoding', 'cache-control': 'public, max-age=5', 'transfer-encoding': 'chunked', 'content-type': 'text/html; charset=utf-8', 'last-modified': 'Mon, 27 Apr 2015 13:16:45 GMT', 'expires': 'Mon, 27 Apr 2015 13:27:26 GMT', 'alternate-protocol': '80:quic,p=1,80:quic,p=1'}>
>>> if 'server' in h.keys():
...     >>> webbrowser.open_new('www.pythonlake.com')
True
>>>


The method display the keys of the dictionary.
 >>> dict={'Adil':49,'John':33}
>>> dict.keys()
dict_keys(['John', 'Adil'])

The method returns with:
 >>> x=dict({'VA':3.1, 'DC':2,'NY':5.1, 'CA':4.4})
>>> x.keys()
dict_keys(['VA', 'DC', 'NY', 'CA'])
>>>

Comments