Reputation: 31
Here is my code
crel = {('hanso', 'unif', 'manjo', 'sandi'): ['coiu','lemin'],('yyy', 'qe', 'tgg', 'ijg'):['KOH','TYH']}
key = 'tgg'
for k, v in crel.items():
if key in k:
print(v)
else:
print('no result')
Output:
no result
['KOH', 'TYH']
I want to use one of tuples to get all the values in the list, but there is always "no result" comes out along with my desired result.
I know it's because I use "for" loop, but I have tried really hard to avoid this happening.
How do I use 'tgg' to get the result like this:
['KOH', 'TYH']
Or, use 'hanso' to get the result like this:
['coiu','lemin']
Could someone please help me use one of the tuples and show the corresponding value? When there is really not a match, show 'no result'
Upvotes: 1
Views: 30
Reputation: 1837
crel = {('hanso', 'unif', 'manjo', 'sandi'): ['coiu', 'lemin'],
('yyy', 'qe', 'tgg', 'ijg'): ['KOH', 'TYH']}
key = 'tgg'
values = [crel[k] for k in crel.keys() if key in k]
if len(values) > 0:
for val in values:
print(val)
else:
print("no result")
Upvotes: 1