list_a-list_b

Tim Peters tim.one at comcast.net
Wed Apr 17 01:54:56 EDT 2002


[David Eppstein]
> ...
> Better is to convert b to a dictionary
> (where are dict comprehensions when you need them):
>
>     dict_b = {}
>     for x in list_b: dict_b[x] = 1

Looks clear enough to me, although

    for x in list_b:
        dict_b[x] = 1

is clearer.  If you like, you can do this:

    dict([(x, 1) for x in list_b])

or this:

    dict(zip(list_b, [1]*len(list_b)))

or even:

    dict(zip(list_b, list_b))

since you don't really care what the dict's values are, just the keys.  I
draw the line before:

    dict(zip(*[list_b]*2))

though.  Contrary to popular belief, there's no Fabulous Prize awaiting
those who minimize either vertical or horizontal space <wink>.






More information about the Python-list mailing list