0

I have been working on operations of dictionaries . In the below code, I want to extract the key from the value. How to do it in simpler way?

dict= { "philips": "bulb", "cisca": "led"}

print(dict.values()

I want to access "philips " from "bulb"

Thankyou

Aditi
  • 53
  • 8

2 Answers2

2
print([k for k,v in dict.items() if v=='bulb'])

please note that you can have multiple keys with the same value.

ncica
  • 7,015
  • 1
  • 15
  • 37
Lior Cohen
  • 5,570
  • 2
  • 14
  • 30
1

Check this question: Python: Best Way to Exchange Keys with Values in a Dictionary?

Based on this answer:

>>> a = {'philips': 'bulb', 'cisca': 'led'}
>>> res = dict(zip(a.values(), a.keys()))
>>> res["bulb"]
'philips'
funk
  • 2,221
  • 1
  • 24
  • 23