Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python dictionary get key by value

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)
# []
Comment

get value and key from dict python

myDict = {
    "message": "Hello Grepper!"
}
for key, value in myDict.items():
    print(key)      #Output: message
    print(value)    #Output: Hello Grepper!
Comment

get key from value dictionary py

dict = {1: 'a', 2: 'b'}
value = 'a'
key = [x for x in dict.keys() if dict[x] == value][0]
Comment

get key from value in python dict

list(prio.keys())[list(prio.values()).index(x)] 
Comment

dict ;get a key of a value

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)
Comment

get a value using a dictionary key in python

print(d.get('key5', 'NO KEY'))
# NO KEY

print(d.get('key5', 100))
# 100
Comment

return key from value dictionary python

cars = {'ford': 10, 'opel': 5 }

def get_val(key):
    return cars[key]

ford = get_val('ford')
print(ford)
Comment

PREVIOUS NEXT
Code Example
Python :: how to add up everything in a list python 
Python :: how to find location using latitude and longitude in python dataframe 
Python :: scikit learn split data set 
Python :: pil image base64 
Python :: python previous answer 
Python :: random word python 
Python :: python print 
Python :: django sort queryset 
Python :: creating virtual environment python 
Python :: how to convert a pandas series from int to float in python 
Python :: Python - Drop row if two columns are NaN 
Python :: parameter grid 
Python :: ready command discord.py 
Python :: convert any base to decimal python 
Python :: dataframe delete row 
Python :: sqlalchemy create engine PostgreSQL 
Python :: torchviz 
Python :: python exe not working on other pc 
Python :: python logging to file 
Python :: python iterate over multidimensional dictionary 
Python :: openpyxl add worksheet 
Python :: urllib.request headers 
Python :: print python 
Python :: add rectangle matplotlib 
Python :: pandas absolute value 
Python :: what is my python working directory 
Python :: how to find csrf token python 
Python :: mad scipy 
Python :: discord get username slash command 
Python :: python pip install 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =