Quantcast
Channel: Get key by value in dictionary - Stack Overflow
Browsing latest articles
Browse All 44 View Live

Answer by AD - Stop Putin - for Get key by value in dictionary

As someone mentioned there might be more than one key that have the same value, like my_dict below. Moreover, there might be no matching key.my_dict ={'k1':1,'k2':2, 'k3':1, 'k4':12, 'k5':1, 'k6':1,...

View Article



Answer by rmd0001 for Get key by value in dictionary

One line solution using list comprehension, which returns multiple keys if the value is possibly present multiple times.[key for key,value in mydict.items() if value == 16]

View Article

Answer by Khaled Eid for Get key by value in dictionary

dict_a = {'length': 5, 'width': 9, 'height': 4}# get the key of specific value 5key_of_value = list(dict_a)[list(dict_a.values()).index(5)]print(key_of_value) # length# get the key of minimum...

View Article

Answer by Andrew Anderson for Get key by value in dictionary

I was looking for this same question and I ended up with my variant:found_key = [a[0] for a in dict.items() if a[1] == 'value'][0]Only for those situations when a key has a unique value (which was my...

View Article

Answer by John for Get key by value in dictionary

This is kind of a strange question because the very first comment provides a perfect answer.Based on the sample data example provideddictionary = {'george': 16, 'amber':...

View Article


Answer by bravhek for Get key by value in dictionary

Heres a truly "Reversible Dictionary", Based upon Adam Acosta's solution, but enforcing val-to-key calls to be unique and easily return key from value:from collections import UserDictclass...

View Article

Answer by Frank Wang for Get key by value in dictionary

I glimpsed all answers and none mentioned simply using list comprehension?This Pythonic one-line solution can return all keys for any number of given values (tested in Python 3.9.1):>>>...

View Article

Answer by Adam Acosta for Get key by value in dictionary

I realize it's been a long time and the original asker likely no longer has any need of an answer, but none of these are good answers if you actually have control over this code. You're just using the...

View Article


Answer by pgalilea for Get key by value in dictionary

I ended up doing it with a function. This way you might avoid doing the full loop, and the intuition says that it should be faster than other solutions presented.def get_key_from_value(my_dict,...

View Article


Answer by Carson for Get key by value in dictionary

my_dict = {'A': 19, 'B': 28, 'carson': 28}search_age = 28take only onename = next((name for name, age in my_dict.items() if age == search_age), None)print(name) # 'B'get multiple dataname_list = [name...

View Article

Answer by Mansur Ul Hasan for Get key by value in dictionary

In my case the easiest way is to instantiate disctionary in your code then you can call keys from it like belowhere is my class having dictionary class Config:def local(self): return {"temp_dir":...

View Article

Answer by Bishwas Mishra for Get key by value in dictionary

Just my answer in lambda and filter.filter( lambda x, dictionary=dictionary, search_age=int(search_age): dictionary[x] == search_age , dictionary )

View Article

Answer by Ozkan Serttas for Get key by value in dictionary

I tried to read as many solutions as I can to prevent giving duplicate answer. However, if you are working on a dictionary which values are contained in lists and if you want to get keys that have a...

View Article


Answer by johnaphun for Get key by value in dictionary

Try this one-liner to reverse a dictionary:reversed_dictionary = dict(map(reversed, dictionary.items()))

View Article

Answer by Rafael Valero for Get key by value in dictionary

I found this answer very effective but not very easy to read for me.To make it more clear you can invert the key and the value of a dictionary. This is make the keys values and the values keys, as seen...

View Article


Answer by Cesar for Get key by value in dictionary

dictionary = {'george' : 16, 'amber' : 19}search_age = raw_input("Provide age")key = [filter( lambda x: dictionary[x] == k , dictionary ),[None]][0] # key = None from [None] which is a safeguard for...

View Article

Answer by Sakhri Houssem for Get key by value in dictionary

we can get the Key of dict by :def getKey(dct,value): return [key for key in dct if (dct[key] == value)]

View Article


Answer by Brett for Get key by value in dictionary

get_key = lambda v, d: next(k for k in d if d[k] is v)

View Article

Answer by Axel for Get key by value in dictionary

Consider using Pandas. As stated in William McKinney's "Python for Data Analysis'Another way to think about a Series is as a fixed-length, ordered dict, as it is a mapping of index values to data...

View Article

Answer by Meilin He for Get key by value in dictionary

A simple way to do this could be:list = {'george':16,'amber':19}search_age = raw_input("Provide age")for age in list.values(): name = list[list==search_age].key().tolist() print nameThis will return a...

View Article

Answer by Jelen for Get key by value in dictionary

a = {'a':1,'b':2,'c':3}{v:k for k, v in a.items()}[1]or better{k:v for k, v in a.items() if v == 1}

View Article


Answer by Andriy Ivaneyko for Get key by value in dictionary

You can get key by using dict.keys(), dict.values() and list.index() methods, see code samples below:names_dict = {'george':16,'amber':19}search_age = int(raw_input("Provide age"))key =...

View Article


Answer by Raj Damani for Get key by value in dictionary

def get_Value(dic,value): for name in dic: if dic[name] == value: del dic[name] return name

View Article

Answer by Shishir for Get key by value in dictionary

Here, recover_key takes dictionary and value to find in dictionary. We then loop over the keys in dictionary and make a comparison with that of value and return that particular key.def...

View Article

Answer by hamidfzm for Get key by value in dictionary

You need to use a dictionary and reverse of that dictionary. It means you need another data structure. If you are in python 3, use enum module but if you are using python 2.7 use enum34 which is back...

View Article


Answer by user4805123 for Get key by value in dictionary

This is how you access the dictionary to do what you want:list = {'george': 16, 'amber': 19}search_age = raw_input("Provide age")for age in list: if list[age] == search_age: print ageof course, your...

View Article

Answer by Safia Abdalla for Get key by value in dictionary

If you want to find the key by the value, you can use a dictionary comprehension to create a lookup dictionary and then use that to find the key from the value.lookup = {value: key for key, value in...

View Article

Answer by Ethan for Get key by value in dictionary

There is no easy way to find a key in a list by 'looking up' the value. However, if you know the value, iterating through the keys, you can look up values in the dictionary by the element. If...

View Article

Answer by eold for Get key by value in dictionary

Here is a solution which works both in Python 2 and Python 3:dict((v, k) for k, v in list.items())[search_age]The part until [search_age] constructs the reverse dictionary (where values are keys and...

View Article



Answer by Denis Kutlubaev for Get key by value in dictionary

Sometimes int() may be needed:titleDic = {'Фильмы':1, 'Музыка':2}def categoryTitleForNumber(self, num): search_title = '' for title, titleNum in self.titleDic.items(): if int(titleNum) == int(num):...

View Article

Answer by Jeroen for Get key by value in dictionary

d= {'george':16,'amber':19}dict((v,k) for k,v in d.items()).get(16)The output is as follows:-> prints george

View Article

Answer by user3649211 for Get key by value in dictionary

here is my take on it. This is good for displaying multiple results just in case you need one. So I added the list as well myList = {'george':16,'amber':19, 'rachel':19, 'david':15 } #Setting the...

View Article

Answer by Dan Ahlquist for Get key by value in dictionary

Cat Plus Plus mentioned that this isn't how a dictionary is intended to be used. Here's why:The definition of a dictionary is analogous to that of a mapping in mathematics. In this case, a dict is a...

View Article


Answer by Corley Brigman for Get key by value in dictionary

already been answered, but since several people mentioned reversing the dictionary, here's how you do it in one line (assuming 1:1 mapping) and some various perf data:python 2.6:reversedict =...

View Article

Answer by fanny for Get key by value in dictionary

one line version: (i is an old dictionary, p is a reversed dictionary)explanation : i.keys() and i.values() returns two lists with keys and values of the dictionary respectively. The zip function has...

View Article

Answer by user2457843 for Get key by value in dictionary

Here is my take on this problem. :)I have just started learning Python, so I call this:"The Understandable for beginners" solution.#Code without comments.list1 = {'george':16,'amber':19,...

View Article


Answer by formiaczek for Get key by value in dictionary

it's answered, but it could be done with a fancy 'map/reduce' use, e.g.:def find_key(value, dictionary): return reduce(lambda x, y: x if x is not None else y, map(lambda x: x[0] if x[1] == value else...

View Article


Answer by Patrick for Get key by value in dictionary

I thought it would be interesting to point out which methods are the quickest, and in what scenario:Here's some tests I ran (on a 2012 MacBook Pro)def method1(dict, search_age): for name, age in...

View Article

Answer by Stênio Elson for Get key by value in dictionary

mydict = {'george': 16, 'amber': 19}print mydict.keys()[mydict.values().index(16)] # Prints georgeOr in Python 3.x:mydict = {'george': 16, 'amber':...

View Article

Answer by patrick for Get key by value in dictionary

for name in mydict: if mydict[name] == search_age: print(name) #or do something else with it. #if in a function append to a temporary list, #then after the loop return the list

View Article

Answer by faham for Get key by value in dictionary

key = next((k for k in my_dict if my_dict[k] == val), None)

View Article


Answer by Cat Plus Plus for Get key by value in dictionary

There is none. dict is not intended to be used this way.dictionary = {'george': 16, 'amber': 19}search_age = input("Provide age")for name, age in dictionary.items(): # for name, age in...

View Article

Answer by agf for Get key by value in dictionary

If you want both the name and the age, you should be using .items() which gives you key (key, value) tuples:for name, age in mydict.items(): if age == search_age: print nameYou can unpack the tuple...

View Article


Get key by value in dictionary

I made a function which will look up ages in a Dictionary and show the matching name:dictionary = {'george' : 16, 'amber' : 19}search_age = raw_input("Provide age")for age in dictionary.values(): if...

View Article
Browsing latest articles
Browse All 44 View Live


Latest Images