Updating Arrays with Slices
Introduction
Welcome back to Accessing Elements the NumPy Way. We’re now in Lesson 2, building on the slice-reading skills from the last lesson. Before, we used a slice to select part of an array; now we’ll put a slice on the left side of = to change those positions. We’ll practice filling a range with one value, updating a stepped slice, and replacing a slice with a matching sequence. We’ll also look at what happens when a decimal is assigned to an integer array.
Filling a Contiguous Slice with One Value
As you may recall from the previous lesson, scores[2:5] selects positions 2, 3, and 4: the start is included, and the stop is not. We can use that same slice on the left side of an assignment to change those positions. Assigning one value fills the entire selected slice, so there is no need to write a loop or assign to each position separately. As in every lesson, we start by importing NumPy under the short name np; the later snippets in this lesson continue from this setup.
The assignment changes scores itself, replacing the three selected scores with 0. This is called modifying the array in place: we update the existing array rather than creating a separate one. The result is:
Filling a Strided Slice with a Step
We can also assign through a slice with a step. In the previous lesson, temps[::2] selected every other temperature, starting at position 0. We can apply the same pattern to a new array, lights: putting lights[::2] on the left side of = updates those selected positions instead of reading them. This is useful when the positions we want to change are spaced apart rather than next to each other.
Here, positions 0, 2, 4, and 6 are set to 0; the other positions remain 1. One assignment switches off every other seat light:
Assigning a Matching Sequence to a Slice
The right side of a slice assignment can also be a sequence of replacement values. Imagine two neighboring sensor readings are clearly wrong. We can select exactly those positions and supply one corrected value for each. The number of replacement values should match the number of selected positions, so each position receives a corresponding replacement.
The slice readings[2:4] selects positions 2 and 3. The two values in the list replace the two incorrect readings, while the other readings remain unchanged:
Silent Truncation: Assigning Floats into Integer Arrays
The stock array contains integers, so its dtype is an integer type. When we assign a decimal value to those positions, NumPy stores an integer instead. It truncates the decimal part rather than rounding it: 5.9 becomes 5. Because this happens without an error or warning, check the result when the type matters.
The slice selects positions 1 and 2, and both become 5. Printing the dtype confirms that the array remains an integer array:
Conclusion and Next Steps
We can use a slice on the left side of = to change several array positions in one statement. Assign one value to fill a contiguous or stepped slice, or assign a matching sequence when each selected position needs a different replacement. Remember the dtype surprise: assigning 5.9 to an integer array stores 5, not 6, because NumPy truncates the decimal part. In the upcoming practice, we’ll try these patterns with exam scores, seat lights, sensor readings, and shelf stock, then check each result for ourselves.
