A sub-range of a 6-element list copied by an index loop, then extracted with slice syntax. The replay shows i stepping from start to stop - 1 and sliced growing one element per iteration.

By hand

With NumPy

a[start:stop] returns a view of elements at indices start through stop - 1 without an explicit loop. The snapshot shows the sliced array's shape, dtype, and values.

naive.py
values = [10, 20, 30, 40, 50, 60]
start = 1
stop = 4
sliced = []
for i in range(start, stop):
    sliced.append(values[i])
print('RESULT:', sliced)
library.py
import numpy as np

values = [10, 20, 30, 40, 50, 60]
start = 1
stop = 4
a = np.array(values)
sliced = a[start:stop]
print('shape:', sliced.shape)
print('dtype:', sliced.dtype)
print('values:', sliced.tolist())
print('RESULT:', sliced.tolist())
shape: (3,)
dtype: int64
values: [20, 30, 40]
RESULT: [20, 30, 40]

Implementation notes

  • A NumPy slice returns a view into the original buffer — no copy is made. Modifying sliced[0] also modifies a[start]. Call .copy() on the slice to get an independent array.
  • Python list slicing (values[1:4]) always produces a copy. The "By hand" loop makes this copy explicit element-by-element.
  • Shape, dtype, and values are shown explicitly here because ndarray.__repr__ output varies with NumPy version and print options.