Howto find dict members from a list of keys

  • Thread starter Alexander Eisenhuth
  • Start date
G

Gabriel Genellina

En Thu, 08 Mar 2007 05:20:20 -0300, Alexander Eisenhuth
what algo do you use, when you want to find the dict values from d, with
members
of l. Following example:
d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}
l = [7,8]
found_dic_members = <yourCode>
print found_dict_members
[8,9]

found_dic_members = [d[key] for key in l]
 
S

Steven D'Aprano

Hello,

what algo do you use, when you want to find the dict values from d, with members
of l. Following example:
d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}
l = [7,8]
found_dic_members = <yourCode>
print found_dict_members
[8,9]

It depends.

If all of the values in d are usable as keys (that is, all the values
are hashable, so no lists or sets etc.) then you can build a
reverse-lookup dictionary and use that for fast look-ups.
{'a': 1, 'c': 3, 'b': 2}

But if you can't do that, you're stuck with a slow search through the
entire dict:

def reverse_search(d, target):
"""Return the first key of dict d that matches the target value."""
for key, value in d.iteritems():
if value == target:
return key
raise KeyError('no key found matching that value')


In both cases though, you have to think about what you want to happen for
duplicated values.
 
S

Steven D'Aprano

En Thu, 08 Mar 2007 05:20:20 -0300, Alexander Eisenhuth
what algo do you use, when you want to find the dict values from d, with
members
of l. Following example:
d = {1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10}
l = [7,8]
found_dic_members = <yourCode>
print found_dict_members
[8,9]

found_dic_members = [d[key] for key in l]

*self stares at the line of code*

*self thinks about what he just posted*

*self realises with acute embarrassment that he's jumped to conclusions
and completely misunderstood the Original Poster's question*

Oops! I'll just be slinking away now...
 
G

Gabriel Genellina

En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano
found_dic_members = [d[key] for key in l]

*self stares at the line of code*

*self thinks about what he just posted*

*self realises with acute embarrassment that he's jumped to conclusions
and completely misunderstood the Original Poster's question*

Oops! I'll just be slinking away now...

LOL! :)

(...now! But when I saw your previous post, I had to check whether it was
*me* who misunderstood the OP answering with a silly one-liner...)
 
J

James Stroud

Gabriel said:
En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano
found_dic_members = [d[key] for key in l]

*self stares at the line of code*

*self thinks about what he just posted*

*self realises with acute embarrassment that he's jumped to conclusions
and completely misunderstood the Original Poster's question*

Oops! I'll just be slinking away now...

LOL! :)

(...now! But when I saw your previous post, I had to check whether it
was *me* who misunderstood the OP answering with a silly one-liner...)


--Gabriel Genellina
I think it was a silly one-liner.

If you look closely at the example, the only mapping of

[7, 8] ==> [8, 9]

is

[key, key] ==> [value, value]

Look again at the example and consider the positional relationships.

The answer, then, is trivial if not silly:

[d[k] for k in l]

It doesn't appear he as asking for the interesting case:

[value, value] ==> [key, key]

Its easy to see how Gabriel was thrown for a loop on this one.

James
 
A

Alexander Eisenhuth

Yes it was the silly on-liner ... it was a bit ago I used it last time ... thanks

Gabriel said:
En Thu, 08 Mar 2007 05:37:48 -0300, Steven D'Aprano
found_dic_members = [d[key] for key in l]

*self stares at the line of code*

*self thinks about what he just posted*

*self realises with acute embarrassment that he's jumped to conclusions
and completely misunderstood the Original Poster's question*

Oops! I'll just be slinking away now...

LOL! :)

(...now! But when I saw your previous post, I had to check whether it
was *me* who misunderstood the OP answering with a silly one-liner...)


--Gabriel Genellina
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top