unique unions of several dict keys

Paul Osman posman at eval.ca
Mon Sep 15 12:03:46 EDT 2003


On Mon, 15 Sep 2003 python at sarcastic-horse.com wrote:

> Hi-
>
>
> 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?
>
>
> Thanks.
>
>

well, I'm sure there's a number of ways, here's one:

def unique(keys):
    unique = []
    for i in keys:
        if i not in unique: unique.append(i)
    return unique

a = {"name" : "paul", "age" : 22}
b = {"name" : "paul", "location" : "toronto"}

list = unique(a.keys() + b.keys())
print list

I'm sure there's a better way, but you get the idea. play around with it.

Cheers,

-- 
Paul Osman
posman at eval.ca
http://perl.eval.ca

"Idealists...foolish enough to throw caution
  to the winds...have advanced mankind and have
  enriched the world."
         - Emma Goldman





More information about the Python-list mailing list