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

get a value using a dictionary key in python

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

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

PREVIOUS NEXT
Code Example
Python :: python mysqlclient not installing 
Python :: minimum of two columns in pandas 
Python :: python find closest lower value in list 
Python :: pandas check if value in column is in a list 
Python :: python version command 
Python :: python program for printing fibonacci numbers 
Python :: how to write a file in python 
Python :: python reverse array 
Python :: add a string to each element of a list python 
Python :: Adding new column to existing DataFrame in Pandas by assigning a list 
Python :: python set comparison 
Python :: python string replace index 
Python :: drop missing values in a column pandas 
Python :: get flask version 
Python :: python [remote rejected] master - master (pre-receive hook declined) 
Python :: print output python to file 
Python :: python close database connection 
Python :: python get element from list 
Python :: pandas read column in date format 
Python :: boto signed url 
Python :: python reverse split only once 
Python :: how to change the background of heading in tkinter 
Python :: arch linux python 3.7 
Python :: sorting a dictionary by value in python 
Python :: numpy empty image 
Python :: pandas merge on columns different names 
Python :: pywhatkit 
Python :: add to number in python 
Python :: convert string to dictionary python 
Python :: python convert number in array to integer 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =