Accessing elements of Python dictionary by index

后端 未结 10 1694
说谎
说谎 2020-11-22 08:52

Consider a dict like

mydict = {
  \'Apple\': {\'American\':\'16\', \'Mexican\':10, \'Chinese\':5},
  \'Grapes\':{\'Arabian\':\'25\',\'Indian\':\'20\'} }
         


        
10条回答
  •  抹茶落季
    2020-11-22 09:30

    You can't rely on order on dictionaries. But you may try this:

    dict['Apple'].items()[0][0]
    

    If you want the order to be preserved you may want to use this: http://www.python.org/dev/peps/pep-0372/#ordered-dict-api

提交回复
热议问题