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

1 Answer

0 votes
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': '', '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': '', 'traffic': 10000, "type": "organic"}

>>> print(site_stats.get('site'))
Click here to read more about Python
Click here to read more about Insurance

Related questions

0 votes
asked Dec 19, 2019 in Python by sharadyadav1986
0 votes
asked Aug 30, 2020 in Python by sharadyadav1986