Bird
Raised Fist0
NumPydata~10 mins

np.union1d() for union in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - np.union1d() for union
Start with two arrays
↓
Find unique elements in each array
↓
Combine unique elements
↓
Sort combined elements
↓
Return sorted union array
np.union1d() takes two arrays, finds unique elements in each, combines them, sorts the result, and returns the sorted union.
Execution Sample
NumPy
import numpy as np
arr1 = np.array([1, 3, 5])
arr2 = np.array([3, 4, 5])
result = np.union1d(arr1, arr2)
print(result)
This code finds the sorted union of two arrays arr1 and arr2.
Execution Table
StepActionarr1 uniquearr2 uniqueCombinedSorted UnionOutput
1Start with arr1 and arr2[1, 3, 5][3, 4, 5]
2Find unique elements in arr1[1, 3, 5]
3Find unique elements in arr2[3, 4, 5]
4Combine unique elements[1, 3, 5, 3, 4, 5]
5Sort combined elements[1, 3, 3, 4, 5, 5]
6Remove duplicates and output[1 3 4 5]
7EndExecution stops
💡 All unique elements combined and sorted; duplicates removed; union returned.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
arr1[1, 3, 5][1, 3, 5][1, 3, 5][1, 3, 5][1, 3, 5][1, 3, 5]
arr2[3, 4, 5][3, 4, 5][3, 4, 5][3, 4, 5][3, 4, 5][3, 4, 5]
combined[1, 3, 5, 3, 4, 5][1, 3, 3, 4, 5, 5][1, 3, 4, 5]
result[1 3 4 5]
Key Moments - 3 Insights
Why does the output not include duplicates like 3 and 5 twice?
np.union1d() automatically removes duplicates after combining arrays, as shown in step 6 of the execution_table where duplicates are removed before output.
Is the output always sorted?
Yes, np.union1d() sorts the combined unique elements before returning, as seen in step 5 where the combined array is sorted.
What happens if arrays have no common elements?
The function still combines and sorts all unique elements from both arrays, returning their union without duplicates, similar to the process in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the sorted union array before duplicates are removed?
A[1, 3, 4, 5]
B[1, 3, 3, 4, 5, 5]
C[1, 3, 5]
D[3, 4, 5]
💡 Hint
Check the 'Sorted Union' column at step 5 in the execution_table.
At which step does np.union1d() remove duplicates?
AStep 6
BStep 4
CStep 5
DStep 3
💡 Hint
Look at the 'Action' column in execution_table where duplicates are removed before output.
If arr2 was [6, 7, 8], how would the final output change?
A[6 7 8]
B[1 3 4 5]
C[1 3 5 6 7 8]
D[1 3 5]
💡 Hint
Refer to variable_tracker and think how combining unique elements from arr1 and arr2 changes.
Concept Snapshot
np.union1d(arr1, arr2)
- Combines two arrays
- Finds unique elements in both
- Sorts the combined unique elements
- Returns sorted union array
- Removes duplicates automatically
Full Transcript
np.union1d() takes two arrays and finds their union. It first finds unique elements in each array, then combines them. After combining, it sorts the elements and removes duplicates. The final output is a sorted array containing all unique elements from both input arrays. For example, with arr1=[1,3,5] and arr2=[3,4,5], the union is [1,3,4,5]. This process ensures no duplicates and sorted order in the result.

Practice

(1/5)
1. What does the function np.union1d() do when applied to two arrays?
easy
A. Finds the common elements between two arrays
B. Combines two arrays and returns unique sorted elements
C. Concatenates two arrays without removing duplicates
D. Sorts a single array in descending order

Solution

  1. Step 1: Understand the purpose of np.union1d()

    This function merges two arrays and removes any duplicate values.
  2. Step 2: Check the output characteristics

    The result is sorted and contains only unique elements from both arrays.
  3. Final Answer:

    Combines two arrays and returns unique sorted elements -> Option B
  4. Quick Check:

    Union = unique sorted merge [OK]
Hint: Think of union as merging without duplicates [OK]
Common Mistakes:
  • Confusing union with intersection
  • Assuming duplicates remain
  • Thinking it sorts in descending order
2. Which of the following is the correct syntax to find the union of arrays a and b using numpy?
easy
A. np.union1d[a, b]
B. np.union(a, b)
C. np.union_1d(a, b)
D. np.union1d(a, b)

Solution

  1. Step 1: Recall the correct function name

    The correct numpy function is np.union1d() with parentheses.
  2. Step 2: Check syntax details

    Arguments are passed inside parentheses, not square brackets, and spelling must be exact.
  3. Final Answer:

    np.union1d(a, b) -> Option D
  4. Quick Check:

    Correct function call syntax [OK]
Hint: Use parentheses and exact function name np.union1d() [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Misspelling the function name
  • Using a non-existent function np.union
3. What is the output of the following code?
import numpy as np
x = np.array([1, 3, 5])
y = np.array([3, 4, 5, 6])
result = np.union1d(x, y)
print(result)
medium
A. [1 3 4 5 6]
B. [1 3 5]
C. [3 4 5 6]
D. [1 3 5 3 4 5 6]

Solution

  1. Step 1: Identify unique elements from both arrays

    Array x has [1, 3, 5], array y has [3, 4, 5, 6]. The union combines all unique values.
  2. Step 2: Sort and remove duplicates

    Unique elements combined are [1, 3, 4, 5, 6], sorted in ascending order.
  3. Final Answer:

    [1 3 4 5 6] -> Option A
  4. Quick Check:

    Union = unique sorted merge [OK]
Hint: Union merges unique sorted elements from both arrays [OK]
Common Mistakes:
  • Forgetting to remove duplicates
  • Not sorting the result
  • Printing only one array
4. The following code throws an error. What is the mistake?
import numpy as np
arr1 = [1, 2, 3]
arr2 = [3, 4, 5]
result = np.union1d(arr1 arr2)
print(result)
medium
A. Missing comma between arr1 and arr2 in function call
B. np.union1d() does not accept lists as input
C. np.union1d() requires arrays to be sorted first
D. print() function is used incorrectly

Solution

  1. Step 1: Check function call syntax

    The function call np.union1d(arr1 arr2) is missing a comma between arguments.
  2. Step 2: Confirm input types and print usage

    np.union1d accepts lists or arrays, and print() is correctly used.
  3. Final Answer:

    Missing comma between arr1 and arr2 in function call -> Option A
  4. Quick Check:

    Comma separates arguments [OK]
Hint: Check commas between function arguments [OK]
Common Mistakes:
  • Omitting commas between arguments
  • Thinking input must be numpy arrays only
  • Assuming print() causes error
5. You have two datasets of customer IDs:
dataset1 = np.array([101, 102, 103, 104])
dataset2 = np.array([103, 104, 105, 106])

You want to create a combined list of all unique customer IDs sorted in ascending order. Which code snippet correctly achieves this?
hard
A. combined = np.intersect1d(dataset1, dataset2)
B. combined = np.concatenate((dataset1, dataset2))
C. combined = np.union1d(dataset1, dataset2)
D. combined = np.sort(np.append(dataset1, dataset2))

Solution

  1. Step 1: Understand the goal

    We want all unique customer IDs from both datasets, sorted ascending.
  2. Step 2: Evaluate each option

    combined = np.union1d(dataset1, dataset2) uses np.union1d which merges and sorts unique elements. combined = np.concatenate((dataset1, dataset2)) concatenates but keeps duplicates. combined = np.intersect1d(dataset1, dataset2) finds only common IDs. combined = np.sort(np.append(dataset1, dataset2)) appends and sorts but does not remove duplicates.
  3. Final Answer:

    combined = np.union1d(dataset1, dataset2) -> Option C
  4. Quick Check:

    Union merges unique sorted elements [OK]
Hint: Use np.union1d to merge unique sorted values [OK]
Common Mistakes:
  • Using concatenate without removing duplicates
  • Using intersect1d which finds only common elements
  • Appending and sorting without removing duplicates