itertools.islice() returns an iterator over a slice of any iterable, using the same start, stop and step idea as list slicing. Unlike my_list[2:6], it works on iterators, generators, files and infinite streams, and it produces items lazily instead of building a new list. Use it when you want the first N items, a window, or every Nth item without loading the whole source into memory.
islice is part of the standard library itertools module, so it needs no installation. The stop index is exclusive, exactly like range() and normal slices.
itertools.isliceitertools.isliceislice has two call forms, mirroring range():
| Parameter | Meaning | Default |
|---|---|---|
iterable | Any iterable: list, generator, file object, itertools.count(), ... | required |
start | Index of the first item to return (zero-based) | 0 |
stop | Index to stop before; the item at stop is not returned. None means run to the end | required |
step | Distance between returned items; must be a positive integer | 1 |
Negative values for start, stop or step raise ValueError, because an iterator cannot know its own length or run backwards. If you need items[-3:], convert to a list first or use collections.deque(iterable, maxlen=3).
The result is an iterator, so wrap it in list() or loop over it to see the values.
The first call returns indexes 2, 3, 4 and 5, which are the values 3 to 6. Index 6 is excluded. For a plain list, numbers[2:6] is simpler and faster, so reach for islice when the source is not a sequence.
Stepping works on ranges as well:
Because islice stops pulling items once it reaches stop, it is the standard way to take a finite piece of an endless stream.
Without islice, calling list(fibonacci()) would never finish. The generator only computes the values that are actually requested.
File objects are iterators over lines, so islice gives you a memory-safe "head" and "skip header" tool:
Only the requested lines are read from disk into Python objects, no matter how large the file is.
An iterator can only be read once. islice advances the underlying iterator, and any items it skips are consumed and lost.
A few consequences follow from this:
start is not free. islice(it, 1_000_000, 1_000_010) still steps through the first million items; it is O(start), not a jump.islice does not copy the list, but it also does not use the list's indexing, so it is slower than list[a:b].itertools.tee() or store the items in a list.A common pattern is processing a large iterable in fixed-size batches. From Python 3.12 the standard library has itertools.batched() for exactly this, and it is the better choice on current versions. On older versions you can build it with islice.
batched() yields tuples; the helper above yields lists. Python 3.13 added a strict=True option to batched() that raises an error if the last batch is short.
No. islice(data, 2, 6) returns the items at indexes 2, 3, 4 and 5. The stop position is excluded, as in list slicing and range().
No. Negative start, stop or step values raise ValueError. For the last N items of an iterable, use collections.deque(iterable, maxlen=N).
List slicing needs a sequence and copies the selected items into a new list immediately. islice works on any iterable, returns a lazy iterator, and consumes the source as it goes.
No. It keeps only the current item, so memory use stays constant regardless of the source size. The cost is time: skipped items still have to be generated.
itertools.islice(iterable, start, stop, step) gives you list-style slicing for anything you can iterate over, including generators, files and infinite streams. The stop index is exclusive, negative values are not allowed, and the underlying iterator is consumed as you read. Use it for "first N items", skipping headers and sampling, and prefer itertools.batched() on Python 3.12 or later when you need fixed-size chunks.