Answers for "how to check key and value in dictionary python"

5

how to get the key for a value in a dictionary in python

# 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))
Posted by: Guest on June-12-2020
9

how to know if a key is in a dictionary python

dict = {"key1": 1, "key2": 2}

if "key1" in dict:
Posted by: Guest on September-16-2020
1

python dictionary get value if key exists

val = dict.get(key , defVal)  # defVal is a default value if key does not exist
Posted by: Guest on January-28-2021

Code answers related to "how to check key and value in dictionary python"

Python Answers by Framework

Browse Popular Code Answers by Language