Sorting by item_in_another_list

Paul Rubin http
Wed Oct 25 02:59:57 EDT 2006


ZeD <vito.detullio at gmail.com> writes:
> >   desired_result = B + sorted(x for x in A if x not in B)
> this. is. cool.

Actually it's better to use a set if B is large:

  B2 = set(B)
  desired_result = B + sorted(x for x in A if x not in B2)

That avoids a linear search through B for each element of A.



More information about the Python-list mailing list