unique unions of several dict keys

Jeff Epler jepler at unpythonic.net
Mon Sep 15 12:05:11 EDT 2003


Using python2.3's sets module:
>>> import sets
>>> set = sets.Set
>>> d1 = {1: None, 2: None, 3: None}
>>> d2 = {2: None, 5: None}
>>> s1 = set(d1)
>>> s2 = set(d2)
>>> s1 | s2
Set([1, 2, 3, 5])

Another way, not using sets:
    def all_keys(*dicts):
        ret = {}
        for d in dicts: ret.update(d)
        return ret.keys()

>>> all_keys(d1, d2)
[1, 2, 3, 5]

Jeff





More information about the Python-list mailing list