0

I have a dict and a list like this:

hey = {'2': ['Drink', 'c', 'd'], '1': ['Cook', 'a']}
temp = ['Cook', 'a']

I want to check if temp is present in hey. My code:

def checkArrayItem(source,target):
    global flag
    flag = True

    for item in source:
        if (item in target):
            continue
        else:
            flag = False
            break
for i,arr in enumerate(hey) :
    if (len(temp) == len(hey[arr])):
        checkArrayItem(temp,hey[arr])
        if (flag):
            print('I got it')
            break

What is a more elegant way to do this check?

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Relax ZeroC
  • 651
  • 1
  • 12
  • 27

2 Answers2

2

How about temp in hey.values()?

barak manos
  • 29,648
  • 10
  • 62
  • 114
0

Just use sets to compare to container entirely:

In [40]: hey = {'2': ['Drink', 'c', 'd'], '1': ['Cook', 'a']}

In [41]: temp = {'Cook', 'a'}
# This will give you the keys within your dictionary that their values are equal to tamp
In [42]: [k for k, v in hey.items() if set(v) == temp]
Out[42]: ['1']
Mazdak
  • 105,000
  • 18
  • 159
  • 188