Reputation: 63
dict = {a:[1,2,3,4], b:[4,5,6]}
I would like to create a function func1 that takes in a dictionary as an argument
def func1(dict, elem1, elem2):
and use a simple way to essentially create the following if condition
if ((elem1 ==a and elem2 in dict[a]) or (elem1 == b and elem2 in dict[b]))
the dictionary can have multiple key-value entries
Upvotes: 0
Views: 49
Reputation: 113988
def func1(a_dict, elem1, elem2):
return elem2 in a_dict.get(elem1,[])
I guess?
would yeild the following
data = {'a':[1,2,3,4], 'b':[4,5,6]}
print(func1(data,'a',6)) #False
print(func1(data,'a',2)) #True
print(func1(data,'b',2)) #False
print(func1(data,'b',6)) #True
although im not sure what you expect if elem1 is not in the dictionary... this implementation just returns False
Upvotes: 1