Accessing elements of Python dictionary by index

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

Consider a dict like

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


        
10条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 09:13

    Simple Example to understand how to access elements in the dictionary:-

    Create a Dictionary

    d = {'dog' : 'bark', 'cat' : 'meow' } 
    print(d.get('cat'))
    print(d.get('lion'))
    print(d.get('lion', 'Not in the dictionary'))
    print(d.get('lion', 'NA'))
    print(d.get('dog', 'NA'))
    

    Explore more about Python Dictionaries and learn interactively here...

提交回复
热议问题