Accessing items in lists within dictionary python

前端 未结 3 638
北恋
北恋 2021-02-04 10:47

I have a dictionary that has keys associated with lists.

mydict = {\'fruits\': [\'banana\', \'apple\', \'orange\'],
         \'vegetables\': [\'pepper\', \'carr         


        
3条回答
  •  情书的邮戳
    2021-02-04 11:27

    mydict = {'fruits': ['banana', 'apple', 'orange'],
         'vegetables': ['pepper', 'carrot'], 
         'cheese': ['swiss', 'cheddar', 'brie']}
    
    item = "cheddar"
    if item in mydict['cheese']:
        print ("true")
    

    this works, but you have to reference the keys in the dictionary like cheese, vegetables etc instead because of the way you made the dictionary, hope this helps!

提交回复
热议问题