Move from Lists to Arrays

Use a NumPy array when values form a regular numerical structure and corresponding positions should participate in the same operation.

Python lists can hold almost any mixture of values. That flexibility is useful, but the + operator on lists does not mean numerical addition. NumPy arrays give regular numerical data a different set of operations.

Read the Operation Before the Values

Consider two temperature readings and a correction of 0.5 degrees:

[18.5, 20.0, 0.5, 0.5]

The result is correct for Python lists. List addition concatenates: it joins one list after another. It does not add values in matching positions.

NumPy arrays use + as a numerical operation. Import NumPy with its customary short name, then construct an array from the same readings:

[19.  20.5]

The single value 0.5 is added to every element. The original array remains unchanged because the expression creates a new result:

print(readings)  # [18.5 20. ]

Q1. Predict list addition

What does this expression produce?

[18.5, 20.0] + [0.5, 0.5]
Choose one

Select one choice, then check.

HintRead the operands as lists

List addition uses the same joining rule whether the items are numbers or words.

SolutionThe two lists are joined

The result is [18.5, 20.0, 0.5, 0.5]. The two correction values become new items at the end.

Not attempted
Review

Not marked done.

Represent a Regular Numerical Table

Suppose three observations each contain readings from two sensors:

Every row has the same number of numerical values. This regular structure makes the table a natural array. Adding one correction applies it to all six values:

[[19.  20.5]
 [22.  22.5]
 [19.5 24.5]]

Read the output in the same arrangement as the input. Each value moved by exactly 0.5; no row was appended and no loop was needed.

Q2. Apply one correction to every reading

Complete the array expression so every reading increases by 0.5. Do not change the original array.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintUse the array in a new expression

Assign readings + 0.5 to corrected. There is no need to visit each element separately.

SolutionCreate a new corrected array
corrected = readings + 0.5

The expression computes six additions and returns a new array. It does not assign new values into readings.

Not attempted
Review

Not marked done.

Keep Lists for List-Shaped Work

Arrays do not replace lists. Choose the representation that matches the data and the operation.

SituationNatural starting choiceReason
regular table of numerical readingsarraynumerical operations apply across the table
names collected one at a timelistthe collection can grow and contains ordinary Python values
mixed record such as a name, path, and noterecord or other Python structurethe fields have different roles and types

A list can also be the convenient place where values are first collected. Once the values form a regular numerical structure, np.array(values) can convert that structure for numerical work. The decision is about meaning, not about one type being universally better.

Q3. Choose a representation from the task

Which data is the clearest candidate for a NumPy array at this point?

Choose one

Select one choice, then check.

HintLook for both regularity and numerical work

An array is most useful here when the values form a consistent table and share a numerical role.

SolutionChoose the numerical table

The rectangular table is the clearest array candidate. A growing collection of names remains natural as a list, while mixed named values need a structure that preserves their different roles.

Not attempted
Review

Not marked done.

A Python list joins with +; a NumPy array performs numerical addition. Arrays are a good fit for regular numerical data, while lists remain useful for flexible collections. The next lesson builds small arrays from several clearly stated construction rules.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.