ordered sets operations on lists..

Alex Martelli aleaxit at yahoo.com
Sat Feb 11 13:24:04 EST 2006


Raymond Hettinger <python at rcn.com> wrote:
   ...
> The intersection step is unnecessary, so the answer can be simplified a
> bit:
> 
> >>> filter(set(l2).__contains__, l1)
> [5, 3]
> >>> filter(set(l1).__contains__, l2)
> [3, 5]

...and if one has time to waste, "setification" being only an
optimization, it can also be removed: filter(l2.__contains__, l1) etc
(very slow for long lists, of course).

Personally, I'd always use (depending on guesses regarding lengths of
lists) [x for x in l1 if x in l2] or the setified equivalent, of course.


Alex





More information about the Python-list mailing list