user4062
0
Q:

how to get a key of an item in python

print(dict.get("key"))
1
print(dict["key"])
2
# creating a new dictionary 
my_dict ={"java":100, "python":112, "c":11} 
  
# list out keys and values separately 
key_list = list(my_dict.keys()) 
val_list = list(my_dict.values()) 
  
print(key_list[val_list.index(100)]) 
print(key_list[val_list.index(112)]) 
  
# one-liner 
print(list(my_dict.keys())[list(my_dict.values()).index(112)]) 
0
# function to return key for any value 
def get_key(val): 
    for key, value in my_dict.items(): 
         if val == value: 
             return key 
  
    return "key doesn't exist"
  
# Driver Code 
  
my_dict ={"java":100, "python":112, "c":11} 
  
print(get_key(100)) 
print(get_key(11)) 
3

New to Communities?

Join the community