v9.1.1
Menu

Array Stacking

easyFREEReshaping & StackingNumPy StackingNumPy Foundations

Problem Statement

Combine multiple arrays using stacking operations.

Background

NumPy provides several stacking functions:

  • np.vstack - vertical stack (row-wise)
  • np.hstack - horizontal stack (column-wise)
  • np.concatenate - general concatenation
  • np.stack - stack along new axis

Your Task

Write a function stack_arrays(arr1, arr2) that combines two 1D arrays.

Output Format

Return a dictionary with:

  • "vertical": vstack result (2 rows) as list
  • "horizontal": hstack result (1 longer row) as list
  • "as_columns": Side by side as columns as list

Example

Input
arr1 = [1, 2, 3], arr2 = [4, 5, 6]
Output
{'vertical': [[1, 2, 3], [4, 5, 6]], 'horizontal': [1, 2, 3, 4, 5, 6], 'as_columns': [[1, 4], [2, 5], [3, 6]]}
Reasoning
  • Inputs: arr1 and arr2 are two 1D arrays of the same length n=3n = 3: a=(1,2,3)a = (1, 2, 3) and b=(4,5,6)b = (4, 5, 6). Every result below uses the same six numbers, only arranged differently.
  • vertical: each input becomes one row, aa on top of bb. That gives a 2×32 \times 3 grid, [[1, 2, 3], [4, 5, 6]]: row ii is input ii.
  • horizontal: the two arrays are joined end to end along their only axis, so the result stays 1D with length n+n=6n + n = 6: [1, 2, 3, 4, 5, 6].
  • as_columns: each input becomes one column instead. Row jj pairs the jj-th entries, (aj,bj)(a_j, b_j), giving a 3×23 \times 2 grid [[1, 4], [2, 5], [3, 6]].
  • Notice: as_columns is exactly the transpose of vertical (shape 3×23 \times 2 vs 2×32 \times 3), while horizontal adds no new dimension at all. All three values are plain nested Python lists, not arrays.

Constraints

  • Both arrays have the same length
  • Arrays are 1D

Learn this in

More from NumPy Foundations

solution.py

Test results

Sign in to run your code against the test cases.