Arrays Versus Lists

Python lists are flexible. NumPy arrays are numerical and shaped.

items = [1, "two", 3.0]

A list can hold mixed types. That flexibility is useful, but it is not ideal for numerical computation.

A NumPy array is meant for many values of a consistent kind.

Arithmetic means different things

List addition concatenates:

[1, 2, 3] + [10, 20, 30]

produces:

[1, 2, 3, 10, 20, 30]

Array addition adds element by element:

List addition versus array addition

Runs locally with Python in your browser.

Ready to run.

That one difference explains why arrays become central in ML code. Numerical programs usually need elementwise operations, not list concatenation.

Arrays carry shape

A list can contain rows:

rows = [[1, 2, 3], [4, 5, 6]]

But a NumPy array records the shape directly:

Shape is not decoration. It tells you how the data is organized.

Exercise: List or array addition

What does NumPy array addition usually mean?

Choose one

Select one choice, then check.

Hint

Think about what happens to corresponding positions such as 1 and 10.

Solution

NumPy array addition usually means elementwise addition: values in corresponding positions are added. For example, the first result is 1 + 10 = 11.