v9.1.1
Menu

Create NumPy Array from List

easyFREEArray ManipulationNumPy BasicsNumpy

Problem Statement

Create a NumPy array from a Python list and return its basic properties.

Background

NumPy arrays are the foundation of numerical computing in Python. Unlike Python lists, NumPy arrays are:

  • Homogeneous (all elements same type)
  • More memory efficient
  • Support vectorized operations

Your Task

Write a function create_array(python_list) that:

  1. Converts the input list to a NumPy array
  2. Returns a dictionary with:
    • "array": The array as a list
    • "shape": The shape as a list
    • "dtype": The data type as a string
    • "ndim": Number of dimensions

Output Format

Return a dictionary with exactly these four keys.

Example

Input
[1, 2, 3, 4, 5]
Output
{'array': [1, 2, 3, 4, 5], 'shape': [5], 'dtype': 'int64', 'ndim': 1}
Reasoning
  • Input: python_list is a flat list of five whole numbers, [1,2,3,4,5][1, 2, 3, 4, 5]. Once it becomes an array, every element must share one number type, and since all five are integers that type is a 64-bit integer.
  • array: the values themselves, unchanged and in the same order, so it reads back as [1, 2, 3, 4, 5].
  • shape: the size along each axis. One axis holds 5 elements, so the shape is a single-entry list, [5]. A 2×22 \times 2 nested list such as [[1,2],[3,4]][[1, 2], [3, 4]] would give [2, 2] instead.
  • dtype: the shared element type written as text, 'int64'. If even one entry were a decimal like 1.51.5, every element would become a float and this would read 'float64'.
  • ndim: how many axes there are, which is the number of entries in the shape. Here that is 11.
  • Notice that shape and ndim are linked: ndim is always the length of the shape list.

Constraints

  • Use numpy.array() to create the array
  • Input can be 1D or 2D list
  • Return dtype as string

Learn this in

More from Numpy

solution.py

Test results

Sign in to run your code against the test cases.