I have a dictionary:
x = {'a': [1, 2, 3], 'b': [4, 5, 6]}
and I have to extract a key based on an input. For example assume it is 3
, so my output should be 'a'
.
Here's my code:
x = {'a': [1, 2, 3], 'b': [4, 5, 6]}
n = 3
output = [k for k, v in x.items() if n in v]
print(output[0])
Can it be done more efficiently?
Elements in the lists are unique, i.e. the value 3
will only be in one list.