Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access a dictionary value inside a list?

I would like to access the value of the following :

a = [{'translation_text': 'I love cake.'}]

Desired output:

"I love cake."

I tried the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a['translation_text'] 

and I get the following error:

TypeError: string indices must be integers

Has anyone experienced the same issue before? Thanks a lot for your help!

>Solution :

The dictionary is the first item of the list a, i.e., you need to access it by using its index, 0:

>>> a = [{'translation_text': 'I love cake.'}]
>>> a[0]
{'translation_text': 'I love cake.'}
>>> a[0]['translation_text']
'I love cake.'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading