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

find value in dictionary python

k = {1:10,2:20,3:30,4:40}
#idea : print([5,6,7,8][[12,15,47,45].index(15)])
b = list(k.keys())[list(k.values()).index(20)] #find index of 20 => return 2
Comment

PREVIOUS NEXT
Code Example
Python :: docstring 
Python :: dictionary lookup python 
Python :: python instagram bot 
Python :: fastest way to check odd or even in python 
Python :: how to create models in django 
Python :: re python 
Python :: what does filename = path(file).stem python 
Python :: how to make an argument optional in python 
Python :: how to add number to string in python 
Python :: python newton raphson 
Python :: python tkinter scrollbar 
Python :: pandas add prefix to column names 
Python :: python variable definieren 
Python :: javascript or python 
Python :: python write error to file 
Python :: python multiclass inheritance with inputs 
Python :: sns histplot change legend labels 
Python :: part of a flower 
Python :: python find image on screen 
Python :: optimization in python 
Python :: how to add to end of linked list python 
Python :: extract all file in zip in python 
Python :: print backwards python 
Python :: how to add background and font color to widget in tkinter 
Python :: lowering the time.countdown python 
Python :: networkx - remove small components from a graph 
Python :: when training= false still dropout 
Python :: access dynamicall to name attribute python 
Shell :: pip install django storages 
Shell :: refusing to merge unrelated histories 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =