-2
myDict = {'Fruits': ['Apples', 'Oranges', 'Bananas'], 'Meats': ['Beef', 'Chicken', 'Pork']}
myDict2 = {'FoodType1': ['Fruits'], 'FoodType2': ['Meats']}

What I am trying to do is this:

food = myDict2.get('FoodType1')
if 'Oranges' in myDict.get(food):
   return "True!"

I know the issue is that the variable 'food' is ['Fruits'], which is a mutable list. Is there a better way to do the if check if I must use those two dicts?

I add to myDict2 using myDict2.append() and it looks to be adding them as lists. How can I add them as strings?

Blade
  • 984
  • 3
  • 12
  • 34
user1224478
  • 345
  • 2
  • 5
  • 15

1 Answers1

1

Wrap your dict with curly braces

myDict = {'Fruits': ['apples', 'oranges', 'bananas'], 'Meat': ['beef', 'chicken']}
if 'apples' in myDict['Fruits']:
     print 'Here'
Aaron
  • 2,383
  • 3
  • 22
  • 53
  • Oh, this explains everything - `TypeError` usually appears when the syntax is invalid, hence the name. – vaultah Mar 27 '15 at 16:16
  • No. `SyntaxError` is what you get for invalid syntax -- which is your first line of code gives. I'm not sure how you got a `TypeError`. I presume the code you posted in your question is not the code you are using. – Steven Rumbalski Mar 27 '15 at 16:33