d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
keys = [k for k, v in d.items() if v == 'aaa']
print(keys)
# ['key1', 'key2']
keys = [k for k, v in d.items() if v == 'bbb']
print(keys)
# ['key3']
keys = [k for k, v in d.items() if v == 'xxx']
print(keys)
# []
myDict = {
"message": "Hello Grepper!"
}
for key, value in myDict.items():
print(key) #Output: message
print(value) #Output: Hello Grepper!
dict = {1: 'a', 2: 'b'}
value = 'a'
key = [x for x in dict.keys() if dict[x] == value][0]
list(prio.keys())[list(prio.values()).index(x)]
myDict={"name":"PythonForBeginners","acronym":"PFB"}
print("Dictionary is:")
print(myDict)
dict_items=myDict.items()
print("Given value is:")
myValue="PFB"
print(myValue)
print("Associated Key is:")
for key,value in dict_items:
if value==myValue:
print(key)
print(d.get('key5', 'NO KEY'))
# NO KEY
print(d.get('key5', 100))
# 100
cars = {'ford': 10, 'opel': 5 }
def get_val(key):
return cars[key]
ford = get_val('ford')
print(ford)