Sorry for the poorly worded title but I asked a question earlier about getting a unique list of items from two lists. People told me to make the list -> sets and then union.
The fastest thing you can do is build two sets from the lists and take the union of them. Both set construction from list and set union are implemented in the runtime, in very optimized C, so it is very fast.
In code, if the lists are l1
and l2
, you can do
unique_elems = set(l1) | set(l2)
EDIT: as @kriss notes, extending l1
with l2
is faster. This code however doesn't change l1
, and works also if l1
and l2
are generic iterables.