Back to NumPy Notes
Topic #44

ufunc Set Operations


What is a Set

A set in mathematics is a collection of unique elements.

Sets are used for operations involving frequent intersection, union and difference operations.


Create Sets in NumPy

We can use NumPy's unique() method to find unique elements from any array. E.g. create a set array, but remember that the set arrays should only be 1-D arrays.

Example

  import numpy as np

  arr = np.array([1, 1, 1, 2, 3, 4, 5, 5, 6, 7])

x = np.unique(arr)

print(x)

Finding Union

To find the unique values of two arrays, use the union1d() method.

Example

  import numpy as np

arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])

  newarr = np.union1d(arr1, arr2)

print(newarr)

Finding Intersection

To find only the values that are present in both arrays, use the intersect1d() method.

Example

  import numpy as np

arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])

  newarr = np.intersect1d(arr1, arr2, assume_unique=True)

print(newarr)

Note: the intersect1d() method takes an optional argument assume_unique, which if set to True can speed up computation. It should always be set to True when dealing with sets.


Finding Difference

To find only the values in the first set that is NOT present in the seconds set, use the setdiff1d() method.

Example

  import numpy as np

set1 = np.array([1, 2, 3, 4])
set2 = np.array([3, 4, 5, 6])

  newarr = np.setdiff1d(set1, set2, assume_unique=True)

print(newarr)

Note: the setdiff1d() method takes an optional argument assume_unique, which if set to True can speed up computation. It should always be set to True when dealing with sets.


Finding Symmetric Difference

To find only the values that are NOT present in BOTH sets, use the setxor1d() method.

Example

  import numpy as np

set1 = np.array([1, 2, 3, 4])
set2 = np.array([3, 4, 5, 6])

  newarr = np.setxor1d(set1, set2, assume_unique=True)

print(newarr)

Note: the setxor1d() method takes an optional argument assume_unique, which if set to True can speed up computation. It should always be set to True when dealing with sets.

Want to go beyond the notes?

Join Coding Now Tech Institute's NumPy course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

ufunc Set Operations – FAQs

Quick answers about learning ufunc Set Operations in NumPy.

This free note from Coding Now Tech Institute explains ufunc Set Operations in NumPy — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every NumPy topic on Coding Now Tech Institute, including ufunc Set Operations, is 100% free with no signup required.
With focused practice, most students grasp ufunc Set Operations in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now