Dev Helpers

Python / Sequence techniques

Replace every other list item

Dev Helpers · June 8, 2025

Extended slice assignment edits selected positions in place. With a stride other than one, the replacement length must match the selected positions.

Worked example

values = [1, 2, 3, 4]
values[::2] = [10, 30]
print(values)

Result

[10, 2, 30, 4]

Implementation notes

Editing a slice updates aliased references to the same list. Assigning a new list to a local variable does not update those observers.

Try it yourself

Try an odd-length list and count the selected slots first.

Open the Python example →

Editorial date June 8, 2025 · Published December 6, 2026

Related articles

Run an async function

An async function call creates a coroutine. A top-level runner supplies the event loop that executes it.

Transpose a matrix

Transposing rows into columns assumes a decision about uneven rows. Ordinary zip stops at the shortest input.

Unpack with a remainder

Starred unpacking separates a fixed header from a variable-length tail. It still requires enough elements for the fixed positions.