Re: [Tutor] How to print corresponding keys in Dictionary

2011-10-24 Thread Alan Gauld
On 24/10/11 19:00, Alan Gauld wrote: For fun: def getKeys(aDict, aValue): return [key for key,val in aDict if val == aValue] Oops, that should have been return [key for key,val in aDict.items() if val == aValue] -- Alan G Author of the Learn to Program web site http://www.alan-g.me

Re: [Tutor] How to print corresponding keys in Dictionary

2011-10-24 Thread bob gailer
Another approach is to subclass dict such that each time you add a key:value pair you also create an entry in a reverse dictionary. Then all you need do is lookup in the reverse dictionary. If there are several keys with the same value, then create and extend a list of values in the reverse di

Re: [Tutor] How to print corresponding keys in Dictionary

2011-10-24 Thread Alan Gauld
On 24/10/11 14:18, Dave Angel wrote: def getkey(dictionary, value): for key, val in dictionary.items(): if val == value: return key Note that if there are multiple keys with the same value, my function would get only the first. It wouldn't be hard to modify the function to re

Re: [Tutor] How to print corresponding keys in Dictionary

2011-10-24 Thread Dave Angel
(You forgot to include the list on your reply. Easiest way is to do a reply-all when you're replying) On 10/24/2011 12:21 PM, Praveen Singh wrote: On Mon, Oct 24, 2011 at 9:18 AM, Dave Angel wrote: def getkey(dictionary, value): for key, val in dictionary.items(): if va

Re: [Tutor] How to print corresponding keys in Dictionary

2011-10-24 Thread Dave Angel
On 10/24/2011 09:10 AM, Praveen Singh wrote: In Dictionary- How to print corresponding keys if the values of dictionary is given?? -d={'a':1,'b':2,'c':3} -i can print the corresponding values by using get() method- - d.get('a') -1 What if i have to print reverse??? A dictionary can be viewed

Re: [Tutor] How to print corresponding keys in Dictionary

2011-10-24 Thread Raúl Cumplido
Hi, Keys are unique in a dictionaire but values aren't. What do you want to print if you have the next dictionaire: dict = {'a' : 1, 'b' : 1} If you are using python 2.7 you can use dictionary comprehensions to swap keys for values: >>> d={'a':1,'b':2,'c':3} >>> new_dict = {v : k for k,v in d.i

[Tutor] How to print corresponding keys in Dictionary

2011-10-24 Thread Praveen Singh
In Dictionary- How to print corresponding keys if the values of dictionary is given?? -d={'a':1,'b':2,'c':3} -i can print the corresponding values by using get() method- - d.get('a') -1 What if i have to print reverse??? ___ Tutor maillist - Tutor@pyt