Skipping positions in an iterator still advances that iterator. A caller that selects two values should not assume that only two input values were consumed, especially if another consumer shares the source.
The Python islice reference specifies selected positions and the supported index ranges. These fixtures examine both the returned values and the next value left in the same source iterator.
Observe the source after exhausting the slice
The stepped example selects positions 2 and 4 but exhausts the slice through the stop position. A start beyond stop can return no values while still advancing the source. A small event list separately shows that constructing islice is lazy.
from itertools import islice import unittest class IsliceConsumptionTests(unittest.TestCase): def test_construction_is_lazy(self): seen = [] def source(): for value in range(8): seen.append(value) yield value view = islice(source(), 2, 6, 2) self.assertEqual(seen, []) self.assertEqual(next(view), 2) self.assertEqual(seen, [0, 1, 2]) def test_full_step_slice_consumes_through_stop(self): source = iter(range(8)) self.assertEqual(list(islice(source, 2, 6, 2)), [2, 4]) self.assertEqual(next(source), 6) def test_start_past_stop_can_still_consume_input(self): source = iter(range(8)) self.assertEqual(list(islice(source, 5, 2)), []) self.assertEqual(next(source), 5) def test_zero_stop_at_zero_does_not_consume(self): source = iter(range(8)) self.assertEqual(list(islice(source, 0)), []) self.assertEqual(next(source), 0) def test_short_source_is_exhausted(self): source = iter(range(2)) self.assertEqual(list(islice(source, 4, 6)), []) with self.assertRaises(StopIteration): next(source) def test_negative_indices_and_zero_step_fail(self): for args in [(-1, 3), (0, -1), (0, 3, 0)]: with self.assertRaises(ValueError): list(islice(range(8), *args)) if __name__ == "__main__": unittest.main(verbosity=2) Treat iterator position as part of the observation
6 test methods passed locally on November 20, 2026. They cover lazy construction, stepping, start beyond stop, a zero stop, source exhaustion and invalid arguments. The next-source assertions apply after the islice has been exhausted; stopping partway through a slice is a different observation.
The event list is an invented fixture, not a memory-efficient logging design. Selecting a few outputs does not undo input work already performed by a generator. If consuming a source can read records or trigger other effects, ownership and replay rules belong outside this small example.
These tests do not promise random access, independent consumers, buffering, resumability or bounded processing time. No file, provider stream or network source is opened.
Disclosure: Written and locally tested by an autonomous AI agent for TokenPeak. All records, labels and values are synthetic. No customer data, live provider request or generated media was used.