Creating Arrays
Array from List
A Python list of integers copied element-by-element into a typed float buffer.
The trace shows v taking each integer value and buffer growing one float
at a time — making the typed-copy idea concrete before NumPy hides it.
By hand
Walk each element of values with a for loop. Convert it to float and
append to buffer. This mirrors what a typed array constructor does: it
allocates a fixed-type storage and fills it value by value.
naive.py
Replay: real traced execution (multi-file project)
values = [1, 2, 3, 4, 5, 6]
buffer = []
for v in values:
buffer.append(float(v))
print('RESULT:', buffer)
values ← [1, 2, 3, 4, 5, 6]
1values = [1, 2, 3, 4, 5, 6]2buffer = []values this step[1, 2, 3, 4, 5, 6]valuesbuffer ← []
1values = [1, 2, 3, 4, 5, 6]2buffer = []3for v in values:values this step[]bufferv ← 1
2buffer = []3for v in values:4 buffer.append(float(v))values this step1vbuffer ← [1.0]
3for v in values:4 buffer.append(float(v))5print('RESULT:', buffer)values this step[] → [1.0]bufferv ← 2
2buffer = []3for v in values:4 buffer.append(float(v))values this step1 → 2vbuffer ← [1.0, 2.0]
3for v in values:4 buffer.append(float(v))5print('RESULT:', buffer)values this step[1.0] → [1.0, 2.0]bufferv ← 3
2buffer = []3for v in values:4 buffer.append(float(v))values this step2 → 3vbuffer ← [1.0, 2.0, 3.0]
3for v in values:4 buffer.append(float(v))5print('RESULT:', buffer)values this step[1.0, 2.0] → [1.0, 2.0, 3.0]bufferv ← 4
2buffer = []3for v in values:4 buffer.append(float(v))values this step3 → 4vbuffer ← [1.0, 2.0, 3.0, 4.0]
3for v in values:4 buffer.append(float(v))5print('RESULT:', buffer)values this step[1.0, 2.0, 3.0] → [1.0, 2.0, 3.0, 4.0]bufferv ← 5
2buffer = []3for v in values:4 buffer.append(float(v))values this step4 → 5vbuffer ← [1.0, 2.0, 3.0, 4.0, 5.0]
3for v in values:4 buffer.append(float(v))5print('RESULT:', buffer)values this step[1.0, 2.0, 3.0, 4.0] → [1.0, 2.0, 3.0, 4.0, 5.0]bufferv ← 6
2buffer = []3for v in values:4 buffer.append(float(v))values this step5 → 6vbuffer ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
3for v in values:4 buffer.append(float(v))5print('RESULT:', buffer)values this step[1.0, 2.0, 3.0, 4.0, 5.0] → [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]bufferfor v in values:
2buffer = []3for v in values:4 buffer.append(float(v))stdout ← RESULT: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
4 buffer.append(float(v))5print('RESULT:', buffer)values this stepRESULT: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]stdout
With NumPy
np.array(values, dtype=float) performs the same typed copy in C, returning
a contiguous float64 buffer. The snapshot shows the array's shape, element
type, and values as a Python list.
library.py
import numpy as np
values = [1, 2, 3, 4, 5, 6]
arr = np.array(values, dtype=float)
print('shape:', arr.shape)
print('dtype:', arr.dtype)
print('values:', arr.tolist())
print('RESULT:', arr.tolist())
shape: (6,)
dtype: float64
values: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
RESULT: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
Implementation notes
dtype=floatis shorthand fornp.float64. NumPy stores every element in the same fixed-width type, so arithmetic on the array needs no per-element type dispatch — that is the source of most of NumPy's speed advantage over Python lists.- Omitting
dtypelets NumPy infer the type: an integer list infersint64, a mixed-type list often infersobject. Being explicit withdtype=floatavoids surprises in downstream arithmetic. arr.shapeis a tuple of dimension sizes. A 1-D array of 6 elements has shape(6,). Contrast with the 2-D case:np.array([[1,2],[3,4]]).shapegives(2, 2).- The element-by-element loop in the "By hand" half is the same pattern as
map-transforminpython-data-basics/ch02— both apply a function to each element and collect results. The difference is that NumPy stores the result in a typed C buffer rather than a Python list. - Shape, dtype, and values are shown explicitly here (rather than printing the
array directly) because
ndarray.__repr__output varies with NumPy version and print options such asnp.set_printoptions.