Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (16.4k points)

Take a look at the dict like:

mydict = {

  'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},

  'Grapes':{'Arabian':'25','Indian':'20'} }

How would I access for example a specific element of this dictionary? for example, I might want to print the main component after few formating the first element of Apple which for our situation is 'American' as it were? 

Extra data The above information structure was made by parsing an information file in a python work. Once made anyway it stays as before for that run.

I'm utilizing this information structure in my function. 

So if the document changes, whenever this application is run the substance of the record are extraordinary and consequently the content of this data structure will be diverse however the configuration would be the equivalent. So you see I in my function I don't realize that the principal component in Apple is 'American' or whatever else so I can't straightforwardly utilize 'American' as a key.

1 Answer

0 votes
by (26.4k points)

Given that it is a dictionary you can access it by utilizing the keys. Getting the dictionary which is stored under "Apple", do the accompanying: 

>>> mydict["Apple"]

{'American': '16', 'Mexican': 10, 'Chinese': 5}

Furthermore, getting the number of them are American (16), do like this:

>>> mydict["Apple"]["American"]


Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

Browse Categories