We use np.setdiff1d() to find items that are in one list but not in another. It helps us see what is unique to the first list.
np.setdiff1d() for difference in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
np.setdiff1d(array1, array2, assume_unique=False)array1 and array2 are the two arrays you compare.
The function returns sorted unique values in array1 that are not in array2.
arr1 that are not in arr2. Output will be [1 2].import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([3, 4, 5]) diff = np.setdiff1d(arr1, arr2) print(diff)
arr1 not in arr2.import numpy as np arr1 = np.array(['apple', 'banana', 'cherry']) arr2 = np.array(['banana', 'date']) diff = np.setdiff1d(arr1, arr2) print(diff)
This program finds numbers that are in list_a but missing from list_b. It prints those unique numbers.
import numpy as np # Two lists of numbers list_a = np.array([10, 20, 30, 40, 50]) list_b = np.array([30, 40, 60]) # Find items in list_a not in list_b unique_to_a = np.setdiff1d(list_a, list_b) print("Items in list_a but not in list_b:", unique_to_a)
The result is always sorted and contains unique values only.
If you know your arrays have unique values already, set assume_unique=True for faster results.
np.setdiff1d() helps find what is unique to the first array compared to the second.
It works with numbers, strings, or any comparable data.
The output is sorted and contains no duplicates.
Practice
What does the function np.setdiff1d(arr1, arr2) do?
Solution
Step 1: Understand the function purpose
np.setdiff1d(arr1, arr2)returns elements unique toarr1that are not found inarr2.Step 2: Compare with options
Finds elements inarr1that are not inarr2matches this behavior exactly, while others describe different operations.Final Answer:
Finds elements in arr1 that are not in arr2 -> Option AQuick Check:
Unique elements in arr1 = D [OK]
- Confusing setdiff1d with intersection
- Thinking it merges arrays
- Assuming it sorts in descending order
Which of the following is the correct syntax to find elements in a not in b using np.setdiff1d()?
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])Solution
Step 1: Identify correct parameter order
The first argument is the array to find unique elements from; the second is the array to exclude elements from.Step 2: Match with options
To find elements inanot inb, usenp.setdiff1d(a, b), which is np.setdiff1d(a, b).Final Answer:
np.setdiff1d(a, b) -> Option DQuick Check:
First array minus second array = A [OK]
- Swapping the order of arrays
- Passing only one array
- Trying to add arrays inside setdiff1d
What is the output of the following code?
import numpy as np
x = np.array([5, 3, 9, 1])
y = np.array([3, 7, 1])
result = np.setdiff1d(x, y)
print(result)Solution
Step 1: Identify elements in x not in y
Elements inxare [5, 3, 9, 1]. Elements inyare [3, 7, 1]. The elements inxbut not inyare 5 and 9.Step 2: Check output format
np.setdiff1dreturns a sorted array without duplicates, so output is [5 9].Final Answer:
[5 9] -> Option BQuick Check:
Unique elements in x = A [OK]
- Including elements from second array
- Not sorting output
- Confusing with intersection output
Find the error in this code snippet:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
result = np.setdiff1d(arr1 arr2)
print(result)Solution
Step 1: Check function call syntax
The callnp.setdiff1d(arr1 arr2)is missing a comma betweenarr1andarr2.Step 2: Verify other parts
Arrays are correctly numpy arrays, function name is correct, and print syntax is valid in Python 3.Final Answer:
Missing comma between arguments in setdiff1d -> Option AQuick Check:
Comma separates arguments = C [OK]
- Forgetting commas between parameters
- Thinking numpy arrays are invalid inputs
- Assuming print needs parentheses in Python 3
You have two arrays representing IDs of users who visited two different websites:
site1 = np.array([101, 102, 103, 104, 105])
site2 = np.array([103, 104, 106])How can you find the IDs of users who visited only the first site?
Solution
Step 1: Understand the problem
We want users who visited site1 but not site2.Step 2: Apply np.setdiff1d
Usingnp.setdiff1d(site1, site2)returns elements insite1not insite2.Step 3: Check other options
np.setdiff1d(site2, site1) reverses the order, giving users only in site2. Options C and D find common or combined users, not unique to site1.Final Answer:
np.setdiff1d(site1, site2) -> Option CQuick Check:
Unique to first array = B [OK]
- Swapping arrays order
- Using intersection instead of difference
- Using union which combines all
