get keys with the same values

Nader n.emami at gmail.com
Thu Jun 12 13:48:42 CEST 2008


On Jun 12, 1:35 pm, bearophileH... at lycos.com wrote:
> Nader:
>
> > d = {('a' : 1), ('b' : 3), ('c' : 2),('d' : 3),('e' : 1),('f' : 4)}
> > I will something as :
> > d.keys(where their values are the same)
>
> That's magic.
>
> > With this statement I can get two lists for this example:
> > l1= ['a','e']
> > l2=['b','d']
> > Would somebody tell me how I can do it?
>
> You can create a new dict where the keys are the values of the input
> dict and the values are a list of the keys of the original dict. So
> scanning the keys, values of the input dict, you can fill the second
> dict. Then you can scan the second dict, and create a list that
> contains only value lists longer than one.
>
> Bye,
> bearophile

Is it niet possible with one or two statement, maybe with list
comprehension. For exmple:

l = [(k,v) for k in d.keys() for v in d.values() | en here we need
some extra logic (v = 1)]

I don;t konw how we can define a logic statement in a list
comprehension.
It will be very compact, if it would possible.

Nader



More information about the Python-list mailing list