Quantcast
Channel: Get key by value in dictionary - Stack Overflow
Browsing index pages (44 articles)
↧

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


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


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

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 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 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 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 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 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 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 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 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 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 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 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 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 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
Browsing index pages (44 articles)


Latest Images