How to do an 'inner join' with dictionaries

Michael J. Fromberger Michael.J.Fromberger at Clothing.Dartmouth.EDU
Mon Feb 27 21:52:33 EST 2006


In article <1141047580.492771.97130 at z34g2000cwc.googlegroups.com>,
 cyborg4 at walla.co.il wrote:

> Let's say I have two dictionaries:
> dict1 is  1:23,  2:76,  4:56
> dict2 is  23:A, 76:B,  56:C
> 
> How do I get a dictionary that is
> 1:A, 2:B, 4:C

The following should work:

  j = dict((k, dict2[dict1[k]]) for k in dict1 if dict1[k] in dict2)

Cheers,
-M

-- 
Michael J. Fromberger             | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA



More information about the Python-list mailing list