Set Operations on Arrays

Set operations treat arrays like mathematical sets so you can find shared values, combined values, and differences between two arrays with single functions like intersect1d, union1d, and setdiff1d.

Learn Set Operations on Arrays in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll compute intersections, unions, and differences, and test membership element-by-element with np.isin and np.in1d.

np.intersect1d(a, b) returns the values present in both arrays. np.union1d(a, b) returns every value from either array. Both deduplicate and sort the result, just like sets in math.

np.setdiff1d(a, b) returns the values that are in a but not in b . Order matters: swapping the arguments asks a different question. For "in exactly one of the two", use np.setxor1d .

np.isin(arr, values) asks "is each element of arr present in values ?" and returns a boolean mask shaped like arr — ideal for filtering. np.in1d does the same but always returns a flat result. Use the mask to select matching elements.

Replace ___ with the function that returns values present in both arrays.

Answer: intersect1d — it returns the sorted values present in both arrays.

✅ Fix: put the array you want to keep values from first; use setxor1d for symmetric difference.

✅ Fix: write np.isin(items, allowed) — the first argument is what you test, the second is the set to test against.

❌ Surprised the output is sorted and deduplicated

Set functions never preserve input order or duplicates.

✅ Fix: if you need original order, build a mask with np.isin and index the original array.

Two shoppers have carts. Find the items they both want, everything either wants, and the items unique to the first cart.

Lesson complete — sets, mastered!

You can find shared values with intersect1d , combine with union1d , subtract with setdiff1d , and test membership with np.isin to filter one array by another.

🚀 Up next: Rounding & Clipping — tidy numbers with round, floor, ceil, and clip.

Practice quiz

What does np.intersect1d(a, b) return?

  • Values present in both arrays
  • All values from either array
  • Values only in a
  • The longer array

Answer: Values present in both arrays. intersect1d returns the sorted unique values present in both arrays.

What does np.union1d(a, b) return?

  • Only shared values
  • Every value from either array, sorted and unique
  • Values only in b
  • The first array

Answer: Every value from either array, sorted and unique. union1d returns all values from either array, deduplicated and sorted.

What does np.setdiff1d(a, b) return?

  • Shared values
  • All values
  • Values in a but not in b
  • Values in b but not in a

Answer: Values in a but not in b. setdiff1d(a, b) returns the values in a that are not in b.

Is np.setdiff1d symmetric?

  • Yes, order never matters
  • Only for sorted input
  • Only for 2D arrays
  • No, swapping arguments changes the result

Answer: No, swapping arguments changes the result. setdiff1d is not symmetric; setdiff1d(a, b) differs from setdiff1d(b, a).

What does np.isin(arr, values) return?

  • A boolean mask shaped like arr
  • A sorted array
  • A count
  • A single boolean

Answer: A boolean mask shaped like arr. isin returns a boolean mask the same shape as arr, ideal for filtering.

How do NumPy set functions treat duplicates in their inputs?

  • They keep all duplicates
  • They remove duplicates (treat input as a set)
  • They raise an error
  • They double them

Answer: They remove duplicates (treat input as a set). Set functions deduplicate, so [3, 3, 1] behaves like the set {1, 3}.

Which function gives values in exactly one of the two arrays?

  • np.intersect1d
  • np.union1d
  • np.setxor1d
  • np.isin

Answer: np.setxor1d. setxor1d returns the symmetric difference: values in exactly one array.

How is the output of np.union1d ordered?

  • Input order
  • Random
  • Sorted ascending
  • Reverse sorted

Answer: Sorted ascending. Set operations always return sorted output.

What is the main difference between np.isin and np.in1d?

  • np.isin keeps the shape of its first argument; in1d returns flat
  • in1d is faster
  • isin only works on strings
  • They give opposite results

Answer: np.isin keeps the shape of its first argument; in1d returns flat. np.isin preserves the shape of the first argument, while in1d always returns a flat array.

How can you keep original order after a membership test?

  • Use union1d
  • Build a mask with np.isin and index the original array
  • Sort first
  • Use setdiff1d

Answer: Build a mask with np.isin and index the original array. Indexing the original array with an np.isin mask preserves its order.