How can i search a variable from a multi key dict and get the respective value in python?
dict1 = {('1700','2700','3700'):'a3g3',('1502','1518'):'a2g3',('2600'):'a3g2'}
var = '1502'
output
should be a2g3
How can i search a variable from a multi key dict and get the respective value in python?
dict1 = {('1700','2700','3700'):'a3g3',('1502','1518'):'a2g3',('2600'):'a3g2'}
var = '1502'
output
should be a2g3
Just iterate over the keys and find
print([dict1[i] for i in dict1.keys() if var in i])
One way:
dict1 = {('1700','2700','3700'): 'a3g3',
('1502','1518'): 'a2g3',
('2600'): 'a3g2'}
print(next(v for k, v in dict1.items() if '1502' in k))
# a2g3
List comprehension is good approach ,
Here is Filter approach just for fun : You can filter the result :
dict1 = {('1700','2700','3700'):'a3g3',('1502','1518'):'a2g3',('2600'):'a3g2'}
var = '1502'
print(dict1[list(filter(lambda x:var in x,dict1.keys()))[0]])
output:
a2g3