lisa21
0
Q:

how to get value from key dict in python

# To get all the keys of a dictionary use 'keys()'
newdict = {1:0, 2:0, 3:0}
newdict.keys()
# Output:
# dict_keys([1, 2, 3])
8
dic = {"A":1, "B":2} 
print(dic.get("A")) 
print(dic.get("C")) 
print(dic.get("C","Not Found ! ")) 
4

# 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)) 
0
a_dict = {"color": "blue", "fruit": "apple", "pet": "dog"}
for key in a_dict:
  print(key, '->', a_dict[key])

# Output:
# color -> blue
# fruit -> apple
# pet -> dog
5
# Python program to show working  
# of keys in Dictionary 
  
# Dictionary with three keys 
Dictionary1 = {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 
  
# Printing keys of dictionary 
print(Dictionary1.keys()) 
  
# Creating empty Dictionary 
empty_Dict1 = {} 
  
# Printing keys of Empty Dictionary 
print(empty_Dict1.keys()) 
1
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
d = {2:"hello"}
d.values()
0
## Values need to be unique

# 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

New to Communities?

Join the community