0
0
NumPydata~5 mins

np.setdiff1d() for difference in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.setdiff1d() do in NumPy?

np.setdiff1d() returns the sorted, unique values in one array that are not in another array. It finds the difference between two arrays.

Click to reveal answer
beginner
How does np.setdiff1d() handle duplicate values in the input arrays?

It removes duplicates and returns only unique values from the first array that are not in the second array.

Click to reveal answer
beginner
Example: What is the output of np.setdiff1d([1, 2, 3, 4], [2, 4])?

The output is [1 3] because 1 and 3 are in the first array but not in the second.

Click to reveal answer
intermediate
Can np.setdiff1d() be used with arrays of strings?

Yes, it works with any array of comparable elements, including strings, returning unique elements from the first array not in the second.

Click to reveal answer
intermediate
Why is the output of np.setdiff1d() always sorted?

Because np.setdiff1d() sorts the unique values before returning them to provide consistent and predictable output.

Click to reveal answer
What does np.setdiff1d(a, b) return?
AAll elements from both arrays combined
BElements in <code>b</code> not in <code>a</code>
CCommon elements in both <code>a</code> and <code>b</code>
DElements in <code>a</code> not in <code>b</code>
If a = [1, 2, 2, 3] and b = [2], what does np.setdiff1d(a, b) return?
A[1, 3]
B[1, 2, 3]
C[2]
D[3]
Is the output of np.setdiff1d() always sorted?
AYes
BNo
COnly if arrays are sorted
DDepends on input type
Can np.setdiff1d() be used with string arrays?
AOnly with boolean arrays
BYes
COnly with numeric arrays
DNo
What happens if all elements of a are in b when using np.setdiff1d(a, b)?
AReturns <code>a</code> unchanged
BReturns <code>b</code>
CReturns an empty array
DRaises an error
Explain how np.setdiff1d() works and give a simple example.
Think about finding what is left in one list after removing items found in another.
You got /4 concepts.
    Describe a real-life situation where you might use np.setdiff1d().
    Imagine you have two lists and want to find what is only in one of them.
    You got /4 concepts.