Bird
Raised Fist0
NumPydata~20 mins

np.concatenate() for joining arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
np.concatenate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of np.concatenate() with 1D arrays
What is the output of this code?
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5])
result = np.concatenate((arr1, arr2))
print(result)
NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5])
result = np.concatenate((arr1, arr2))
print(result)
A[[1 2 3 4 5]]
B
[[1 2 3]
 [4 5]]
C
[1 2 3]
[4 5]
D[1 2 3 4 5]
Attempts:
2 left
💡 Hint
np.concatenate joins arrays along an existing axis. For 1D arrays, it joins elements end to end.
❓ data_output
intermediate
2:00remaining
Shape after concatenating 2D arrays along axis 0
Given two 2D arrays:
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
result = np.concatenate((arr1, arr2), axis=0)
print(result.shape)

What is the shape of the result?
NumPy
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
result = np.concatenate((arr1, arr2), axis=0)
print(result.shape)
A(3, 2)
B(2, 3)
C(3, 3)
D(2, 2)
Attempts:
2 left
💡 Hint
Concatenating along axis 0 stacks rows, so rows add up, columns stay same.
🔧 Debug
advanced
2:00remaining
Error when concatenating arrays with mismatched shapes
What error does this code raise?
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6, 7]])
result = np.concatenate((arr1, arr2), axis=0)
NumPy
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6, 7]])
result = np.concatenate((arr1, arr2), axis=0)
AValueError: all the input array dimensions except for the concatenation axis must match exactly
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CValueError: all the input arrays must have same number of dimensions
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint
Arrays must have matching shapes except along the concatenation axis.
🚀 Application
advanced
2:00remaining
Concatenate arrays along axis 1
What is the output of this code?
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5], [6]])
result = np.concatenate((arr1, arr2), axis=1)
print(result)
NumPy
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5], [6]])
result = np.concatenate((arr1, arr2), axis=1)
print(result)
A
[[1 2]
 [3 4]
 [5 6]]
B
[[1 2]
 [3 4]
 [5]
 [6]]
C
[[1 2 5]
 [3 4 6]]
D[[1 2 3 4 5 6]]
Attempts:
2 left
💡 Hint
Concatenating along axis 1 adds columns side by side.
🧠 Conceptual
expert
2:00remaining
Understanding axis parameter in np.concatenate()
Which statement best describes the role of the axis parameter in np.concatenate()?
AIt specifies the axis along which the arrays will be split before joining.
BIt specifies the axis along which the arrays will be joined, and all other dimensions must match exactly.
CIt determines the number of arrays that can be concatenated at once.
DIt controls whether the arrays are joined vertically or horizontally regardless of their shape.
Attempts:
2 left
💡 Hint
Think about how arrays are stacked and what must be the same for concatenation to work.

Practice

(1/5)
1. What does the function np.concatenate() do in NumPy?
easy
A. Splits an array into multiple smaller arrays
B. Joins two or more arrays along an existing axis
C. Sorts the elements of an array in ascending order
D. Creates a new array filled with zeros

Solution

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

    It is used to join arrays along a specified axis, combining them into one larger array.
  2. Step 2: Compare with other array functions

    Splitting, sorting, or creating zero arrays are different functions like np.split(), np.sort(), and np.zeros().
  3. Final Answer:

    Joins two or more arrays along an existing axis -> Option B
  4. Quick Check:

    np.concatenate() = join arrays [OK]
Hint: Remember: concatenate means join arrays along axis [OK]
Common Mistakes:
  • Confusing concatenate with split or sort functions
  • Thinking it modifies original arrays
  • Assuming it creates arrays from scratch
2. Which of the following is the correct syntax to join two 1D arrays a and b using np.concatenate()?
easy
A. np.concatenate([a, b])
B. np.concatenate(a, axis=1)
C. np.concatenate(a + b)
D. np.concatenate(a, b)

Solution

  1. Step 1: Check the function signature

    np.concatenate() expects a single argument: a sequence (like a list or tuple) of arrays to join.
  2. Step 2: Analyze each option

    np.concatenate([a, b]) correctly passes a list of arrays [a, b]. np.concatenate(a, b) passes two separate arguments, which is invalid. np.concatenate(a + b) tries to add arrays before concatenation, which is not the function's purpose. np.concatenate(a, axis=1) passes axis=1 for 1D arrays, which is invalid.
  3. Final Answer:

    np.concatenate([a, b]) -> Option A
  4. Quick Check:

    Pass arrays as a list to concatenate() [OK]
Hint: Always pass arrays inside a list or tuple to concatenate [OK]
Common Mistakes:
  • Passing arrays as separate arguments instead of a list
  • Using plus (+) operator instead of concatenate
  • Setting wrong axis for 1D arrays
3. What is the output of the following code?
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
result = np.concatenate((x, y), axis=0)
print(result)
medium
A. [[1 2] [3 4] [5 6]]
B. [[1 2 5 6] [3 4 5 6]]
C. [[1 2] [3 4]]
D. Error: shapes do not match

Solution

  1. Step 1: Check shapes of arrays

    x has shape (2, 2), y has shape (1, 2). They match in columns (axis=1) but differ in rows (axis=0).
  2. Step 2: Concatenate along axis=0 (rows)

    Joining along rows stacks y below x, resulting in a (3, 2) array with rows from x then y.
  3. Final Answer:

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

    Concatenate rows stacks arrays vertically [OK]
Hint: Concatenate axis=0 stacks rows; axis=1 stacks columns [OK]
Common Mistakes:
  • Confusing axis=0 and axis=1
  • Expecting horizontal stacking with axis=0
  • Ignoring shape compatibility
4. What is wrong with this code snippet?
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([5, 6, 7])
result = np.concatenate((x, y), axis=0)
print(result)
medium
A. Error because y must be reshaped to 3D
B. No error, output is concatenated array
C. Error because axis=0 is invalid for 2D arrays
D. Error because arrays have incompatible shapes along axis=0

Solution

  1. Step 1: Check shapes of arrays

    x shape is (2, 2), y shape is (3,). They differ in both dimensions.
  2. Step 2: Understand concatenate axis rules

    Arrays must match in all dimensions except the concatenation axis. Here, axis=0 means rows, but y has no matching columns.
  3. Final Answer:

    Error because arrays have incompatible shapes along axis=0 -> Option D
  4. Quick Check:

    Shapes must match except on concat axis [OK]
Hint: Check array shapes match except on concat axis [OK]
Common Mistakes:
  • Ignoring shape mismatch errors
  • Assuming 1D array can concatenate with 2D without reshape
  • Misunderstanding axis parameter
5. You have three arrays:
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
c = np.array([[7, 8]])

You want to join them into one array with shape (4, 2). Which code correctly does this?
hard
A. np.concatenate((a, b), axis=0) + c
B. np.concatenate([a, b, c], axis=1)
C. np.concatenate((a, b, c), axis=0)
D. np.concatenate((a, b, c), axis=2)

Solution

  1. Step 1: Check shapes and desired output

    a shape is (2, 2), b and c are (1, 2). Joining along rows (axis=0) stacks them vertically to get (4, 2).
  2. Step 2: Analyze options

    np.concatenate((a, b, c), axis=0) correctly concatenates all three arrays along axis=0. np.concatenate([a, b, c], axis=1) tries axis=1 which would require matching rows, which is false. np.concatenate((a, b), axis=0) + c adds arrays after concatenation, which is invalid. np.concatenate((a, b, c), axis=2) uses axis=2 which is invalid for 2D arrays.
  3. Final Answer:

    np.concatenate((a, b, c), axis=0) -> Option C
  4. Quick Check:

    Stack arrays vertically with axis=0 [OK]
Hint: Use axis=0 to stack arrays vertically when shapes match columns [OK]
Common Mistakes:
  • Using wrong axis for concatenation
  • Trying to add arrays instead of concatenate
  • Using invalid axis number for 2D arrays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard