๐Ÿ“˜ CodingMarble Learn

Introduction to NumPy: Creating Arrays from Lists

NumPy (Numerical Python) is a library for fast maths on arrays. An array is a grid of items that all have the same type. We make one from a list with np.array(list); a list of lists gives a 2-D array. Attributes ndim, shape, size and dtype describe it. Unlike lists, arrays do maths on every item at once.

๐ŸŽฌ Step-by-step story

  1. A list can mix types. A NumPy array keeps one type in a neat grid.
  2. np.array turns a list into a 1-D array with a shape like (4,).
  3. A list of lists becomes a 2-D array of rows and columns.
  4. All items share one type; mixed items are moved up to a common type.
  5. Maths on an array works on every item at once.
  6. Your turn: pick rows and columns and read the shape.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

๐Ÿค” Common doubts, cleared

Why does print(a) show no commas?

That is how NumPy prints arrays, to show it is an array and not a list. The values are the same.

Why is the shape of a 1-D array written (4,) with a comma?

shape is always a tuple. A tuple with one item needs a trailing comma, so (4,) means "4 items in one dimension".

Is b[1][2] the same as b[1, 2]?

Yes, both give row 1, column 2. The b[1, 2] form is the usual NumPy style and is faster.

Why did my integers turn into 1. and 2.?

One float in the list makes NumPy upcast all items to float, so every number is shown with a decimal point.

Then why use lists at all?

Lists can mix types and grow easily with append. Arrays are best for number crunching on data of one type.

What is NumPy?

NumPy (Numerical Python) is a free Python library (a package of ready-made code) for working with numbers in bulk. Its main object is the ndarray (n-dimensional array).

Install once: pip install numpy. Use it in a program: import numpy as np (np is the usual short name).

An array is a set of items of the same type, stored side by side in memory, reached by index from 0.

Creating a 1-D array from a list

import numpy as np
L = [10, 20, 30, 40]
a = np.array(L)
print(a)          # [10 20 30 40]  (no commas)
print(a[0], a[-1])  # 10 40

You can pass a list directly: np.array([1.5, 2, 3]), or a tuple.

Creating a 2-D array from a list of lists

b = np.array([[1, 2, 3],
              [4, 5, 6]])
print(b[1, 2])    # 6  (row 1, column 2)

Each inner list becomes one row. All inner lists must have the same length, or NumPy cannot make a proper grid.

Array attributes: ndim, shape, size, dtype

One type only: upcasting and dtype

If the list mixes types, NumPy changes all items to one common type: int + float โ†’ float; anything + str โ†’ str. You can also choose: np.array([1, 2, 3], dtype=float) โ†’ [1. 2. 3.].

List vs NumPy array

Other ways to make arrays (good to know): np.zeros(3), np.ones((2, 2)), np.arange(0, 10, 2), np.linspace(0, 1, 5).

Try it: marks as an array

Write the marks of 3 friends in 2 subjects as a list of lists, like [[70, 80], [65, 90], [88, 75]]. Make it an array. Before running, predict shape, ndim and size. Then print m + 5 and m.mean(). Check your shape idea with the sliders in step 6 of the 3D.

Key formulas and definitions

Worked examples

1. a = np.array([5, 10, 15]). Give a.ndim, a.shape, a.size.

ndim = 1, shape = (3,), size = 3.

2. b = np.array([[1, 2], [3, 4], [5, 6]]). Give shape and size, and b[2, 0].

3 rows, 2 columns โ†’ shape (3, 2), size 6. b[2, 0] = 5 (row 2, column 0).

3. What is the dtype of np.array([1, 2, 3.5])?

float64; it prints as [1. 2. 3.5].

4. Compare [2, 4] * 3 and np.array([2, 4]) * 3.

List: [2, 4, 2, 4, 2, 4] (repeated). Array: [6 12] (each item ร— 3).

5. Marks m = np.array([60, 72, 85]). Add 5 grace marks to everyone and find the mean.

m + 5 = [65 77 90]; mean = (65 + 77 + 90) / 3 = 77.33.

6. A 2-D array has shape (4, 5). How many items does it have, and how many are in each row?

size = 4 ร— 5 = 20 items; each row has 5 items.

Common mistakes

Practice quiz

1. Which creates a NumPy array from a list L?
2. Shape of np.array([[1, 2, 3], [4, 5, 6]]) is:
3. np.array([1, 2]) * 2 gives:
4. Items of a NumPy array are:
5. For shape (3, 4), size is:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

How do you create a NumPy array from a list?

import numpy as np, then a = np.array([1, 2, 3]). A list of lists gives a 2-D array.

What is the difference between a list and a NumPy array?

A list can hold mixed types and * repeats it; an array holds one type, does maths element-wise and is faster.

What do shape, ndim and size mean?

shape is (rows, columns), ndim is the number of dimensions, and size is the total number of items.

Where this is taught

CBSE (India)Class 11Introduction to Python

Learn first

Learn next

Related lessons

All Informatics Practices lessons