0 votes
in Python by
How Do You Read From A Dictionary In Python?

1 Answer

0 votes
by
To fetch data from a dictionary, we can directly access using the keys. We can enclose a “key” using brackets […] after mentioning the variable name corresponding to the dictionary.

>>> site_stats = {'site': 'madanswer.com', 'traffic': 10000, "type": "organic"}

>>> print(site_stats["traffic"])

We can even call the get method to fetch the values from a dict. It also let us set a default value. If the key is missing, then the KeyError would occur.

>>> site_stats = {'site': 'madanswer.com', 'traffic': 10000, "type": "organic"}

>>> print(site_stats.get('site'))

Related questions

0 votes
asked Dec 14, 2019 in Python by sheetalkhandelwal
0 votes
asked Jan 10, 2021 in Python by rajeshsharma
...