list_a-list_b
Raymond Hettinger
python at rcn.com
Wed Apr 17 10:10:49 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
[Tim Peters]
> 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))
My favorites:
dict(map(None, list_b, [])) # doesn't loop over list_b twice
or
dict_b = {}
map(dict_b.__setitem__, b, []) # fastest for Py2.2
Raymond Hettinger
More information about the Python-list
mailing list