Bird
Raised Fist0
NumPydata~10 mins

np.intersect1d() for intersection in NumPy - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the common elements between two arrays using numpy.

NumPy
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
common = np.[1](arr1, arr2)
print(common)
Drag options to blanks, or click blank then click option'
Aintersect1d
Bunion1d
Csetdiff1d
Dunique
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.union1d() which returns all unique elements from both arrays.
Using np.setdiff1d() which returns elements in one array but not the other.
2fill in blank
medium

Complete the code to find the intersection of two lists converted to numpy arrays.

NumPy
import numpy as np
list1 = [10, 20, 30, 40]
list2 = [30, 40, 50, 60]
arr1 = np.array(list1)
arr2 = np.array(list2)
result = np.intersect1d(arr1, [1])
print(result)
Drag options to blanks, or click blank then click option'
Alist2
Barr2
Cnp.array(list2)
Dnp.array(list1)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list instead of a numpy array as the second argument.
Passing the first array again instead of the second.
3fill in blank
hard

Fix the error in the code to correctly find the intersection of two arrays.

NumPy
import numpy as np
arr1 = np.array([7, 8, 9])
arr2 = np.array([8, 9, 10])
common = np.intersect1d(arr1, arr2[1])
print(common)
Drag options to blanks, or click blank then click option'
A, assume_unique=True
B, axis=0
C)
D, return_indices=True
Attempts:
3 left
💡 Hint
Common Mistakes
Adding unnecessary parameters that cause errors.
Forgetting to close the function call with a parenthesis.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths only if length is greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Checking if the word string is greater than 3 instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values only for items with values greater than 0.

NumPy
data = {'a': 1, 'b': -1, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
print(result)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys without uppercase.
Using keys as values or vice versa.
Using wrong comparison operators.

Practice

(1/5)
1. What does the function np.intersect1d() do in NumPy?
easy
A. Finds common elements between two arrays
B. Combines two arrays into one
C. Sorts an array in descending order
D. Removes duplicates from an array

Solution

  1. Step 1: Understand the function purpose

    np.intersect1d() is designed to find elements that appear in both input arrays.
  2. Step 2: Compare with other options

    Options A, B, and D describe different functions: sorting descending, combining arrays, and removing duplicates, which are not the purpose of np.intersect1d().
  3. Final Answer:

    Finds common elements between two arrays -> Option A
  4. Quick Check:

    Intersection = common elements [OK]
Hint: Think 'intersection' means shared items [OK]
Common Mistakes:
  • Confusing intersection with concatenation
  • Thinking it sorts descending
  • Mixing with duplicate removal
2. Which of the following is the correct syntax to find the intersection of arrays a and b using NumPy?
easy
A. np.intersect(a, b)
B. np.intersection(a, b)
C. np.intersect1d(a, b)
D. np.intersect1d([a, b])

Solution

  1. Step 1: Recall the correct function name and parameters

    The correct function is np.intersect1d() and it takes two arrays as separate arguments.
  2. Step 2: Check each option

    np.intersect1d(a, b) uses the correct function and syntax. Options B and C use incorrect function names. np.intersect1d([a, b]) incorrectly passes a list of arrays instead of two separate arguments.
  3. Final Answer:

    np.intersect1d(a, b) -> Option C
  4. Quick Check:

    Correct function and parameters [OK]
Hint: Use exact function name and separate arrays as arguments [OK]
Common Mistakes:
  • Using wrong function names
  • Passing arrays inside a list
  • Missing one argument
3. What is the output of the following code?
import numpy as np
x = np.array([3, 1, 4, 1, 5])
y = np.array([5, 9, 2, 6, 5])
print(np.intersect1d(x, y))
medium
A. [1 5]
B. [3 5]
C. [1 5 9]
D. [5]

Solution

  1. Step 1: Identify unique elements in both arrays

    Array x has elements {1, 3, 4, 5} (1 appears twice but counted once). Array y has elements {2, 5, 6, 9} (5 appears twice but counted once).
  2. Step 2: Find common elements

    The only common element between x and y is 5.
  3. Final Answer:

    [5] -> Option D
  4. Quick Check:

    Common elements = [5] [OK]
Hint: Look for numbers appearing in both arrays [OK]
Common Mistakes:
  • Including duplicates
  • Adding elements not in both arrays
  • Confusing order of output
4. The following code throws an error. What is the problem?
import numpy as np
arr1 = [1, 2, 3]
arr2 = [2, 3, 4]
result = np.intersect1d(arr1 arr2)
print(result)
medium
A. Missing comma between arguments in np.intersect1d()
B. Arrays must be NumPy arrays, not lists
C. np.intersect1d() does not accept two arguments
D. print() function is used incorrectly

Solution

  1. Step 1: Check the function call syntax

    The call np.intersect1d(arr1 arr2) is missing a comma between the two arguments.
  2. Step 2: Verify other parts

    Passing lists is allowed because NumPy converts them internally. The function accepts two arguments. The print statement is correct.
  3. Final Answer:

    Missing comma between arguments in np.intersect1d() -> Option A
  4. Quick Check:

    Arguments must be separated by commas [OK]
Hint: Check commas between function arguments [OK]
Common Mistakes:
  • Forgetting commas
  • Thinking lists are invalid inputs
  • Misreading error source
5. You have two arrays representing product IDs sold in two stores:
store1 = np.array([101, 102, 103, 104, 105])
store2 = np.array([104, 105, 106, 107])

How can you find the sorted list of product IDs sold in both stores using np.intersect1d()?
hard
A. np.union1d(store1, store2)
B. np.intersect1d(store1, store2)
C. np.setdiff1d(store1, store2)
D. np.concatenate((store1, store2))

Solution

  1. Step 1: Understand the problem

    We want product IDs common to both stores, which means intersection.
  2. Step 2: Choose the correct function

    np.intersect1d(store1, store2) returns sorted common elements. Other options return union, difference, or concatenation, which are not correct here.
  3. Final Answer:

    np.intersect1d(store1, store2) -> Option B
  4. Quick Check:

    Intersection = common products [OK]
Hint: Use intersect1d for common elements between arrays [OK]
Common Mistakes:
  • Using union instead of intersection
  • Using difference or concatenation
  • Not sorting output