0
0
NumPydata~10 mins

np.union1d() for union in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the union of two arrays using numpy.

NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([3, 4, 5])
result = np.[1](arr1, arr2)
print(result)
Drag options to blanks, or click blank then click option'
Aintersect1d
Bappend
Cunion1d
Dconcatenate
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.intersect1d() which finds common elements instead of union.
Using np.concatenate() which just joins arrays without removing duplicates.
2fill in blank
medium

Complete the code to find the union of two numpy arrays and store it in 'union_result'.

NumPy
import numpy as np
array_a = np.array([10, 20, 30])
array_b = np.array([20, 40, 50])
union_result = np.[1](array_a, array_b)
print(union_result)
Drag options to blanks, or click blank then click option'
Aintersect1d
Bunion1d
Csetdiff1d
Dunique
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.intersect1d() which returns only common elements.
Using np.unique() on one array only.
3fill in blank
hard

Fix the error in the code to correctly compute the union of two numpy arrays.

NumPy
import numpy as np
x = np.array([1, 3, 5])
y = np.array([2, 3, 4])
union = np.[1](x, y)
print(union)
Drag options to blanks, or click blank then click option'
Aunion1d
Bunion
Cunion_1d
Dunion1D
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like 'union' or 'union_1d'.
Capitalizing letters incorrectly in the function name.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = {word: [1] for word in words if [2]
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.
Comparing the word string directly to a number.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.

NumPy
words = ['go', 'run', 'jump', 'fly']
result = [1] = { [2]: [3] for w in words if len(w) > 2 }
Drag options to blanks, or click blank then click option'
Aword_dict
Bw.upper()
Clen(w)
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original list name as the dictionary variable.
Using the word itself instead of uppercase for keys.
Using the word instead of length for values.