Categories

Dec 19, 2019 in Python

Q: How Do You Read From A Dictionary In Python?

1 Answer

Dec 19, 2019
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'))
Click here to read more about Python
Click here to read more about Insurance

Related questions

Madanswer
Dec 19, 2019 in Python
Aug 30 in Python
Dec 22, 2019 in Python
...