biztiger
biztiger

Reputation: 1487

How to access inner dictionary items of a dictionary in Python

I have following data structure:

mydic = { 
           'q4a1_0' : {'title':'4 Question 1 Answer (01)','choices': ((0,'Very Bad'),(1,'Medium'),(2,'Good'),(3,'Very Good'))},
           'q3a1_0' : {'title':'3 Question 1 Answer (01)','choices': ((0,'Very Bad'),(1,'Good'),(2,'Very Good'))}
         }

My question is how can I access 'choices' or 'titles' of any dictionary keys(say 'q4a1_0') directly.

Upvotes: 2

Views: 289

Answers (3)

John Szakmeister
John Szakmeister

Reputation: 47012

mydic is a dictionary, which has a dictionary in it. So mydic['q4a1_0'] gets you to the inner dictionary, and mydic['q4a1_0']['title'] would get you the title key from q4a1_0.

Upvotes: 2

dersvenhesse
dersvenhesse

Reputation: 6404

Access by using the indices.

>>> mydic['q4a1_0']['choices']
((0, 'Very Bad'), (1, 'Medium'), (2, 'Good'), (3, 'Very Good'))

Upvotes: 3

NPE
NPE

Reputation: 500207

You can do it like so:

mydic['q4a1_0']['choices']

Upvotes: 2

Related Questions