> 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
>>> d1={1:23,2:76,4:56} >>> d2={23:"a", 76:"b", 56:"c"} >>> result = dict([(d1k,d2[d1v]) for (d1k, d1v) in d1.items()]) >>> result {1: 'a', 2: 'b', 4: 'c'} If you don't know that all the values in d1 will be in d2, >>> d1[5]=444 >>> d2[333]='qqq' you can put in a "if" check like >>> result = dict([(d1k,d2[d1v]) for (d1k, d1v) in d1.items() if d1v in d2]) {1: 'a', 2: 'b', 4: 'c'} (using "d1v" for "dictionary1's value" and "d1k" for "dictionary1's key") HTH, -tkc -- http://mail.python.org/mailman/listinfo/python-list