Extracting list of keys from 2-key dictionary

Justin Sheehy dworkin at ccs.neu.edu
Mon Dec 13 12:29:14 EST 1999


mdfranz at my-deja.com writes:

> Is there a quicker/easier way in python than the snippet below to do
> this?
> 
>    for a,b in dict.keys():
>         if dict.has_key(x,b):
>               if b not in list:
>                       list.append(b)

Sure.  I assume that just above this code you have:

list = []

This is one way to do it, using a variation on the idiom that I have
adopted for building lists without duplicates in Python:

xdict = {}
for a,b in dict.keys():
    if dict.has_key(x,b):
        xdict[b] = 1
list = xdict.keys()

-Justin

 




More information about the Python-list mailing list