Logo

Programming-Idioms

  • C++
  • Python

Idiom #218 List intersection

Create the list c containing all unique elements that are contained in both lists a and b.
c should not contain any duplicates, even if a and b do.
The order of c doesn't matter.

c = [*{*a} & {*b}]
c = list(set(a).intersection(b))

This avoids converting b into a set prior to the intersection.
c = list(set(a) & set(b))
(def c (clojure.set/intersection (set a) (set b)))

New implementation...
< >
programming-idioms.org