unique unions of several dict keys

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Sep 15 18:26:44 CEST 2003


python at sarcastic-horse.com wrote in
news:mailman.1063641206.10374.python-list at python.org: 

> I have several different dictionaries.  I want to make a unique list
> of all the keys in all the dictionaries.  What would be the best way
> of doing that?

Create a new empty dictionary then use the update method on it for each of 
other dictionaries. The keys of that dictionary are the values you want.

e.g.

>>> def UniqueKeys(*dicts):
    tmpDict = {}
    for d in dicts:
        tmpDict.update(d)
    return tmpDict.keys()

>>> print UniqueKeys({ 'a':1, 'b': 2 }, {'b': 3, 'c': 4})

['a', 'c', 'b']
>>> 

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list