Python itertools.islice(): Slice Iterators Lazily

itertools.islice() slices an iterable lazily. It works like sequence slicing for positive start, stop, and step values, but it returns an iterator instead of building a full list.

from itertools import islice

result = islice(iterable, start, stop, step)

Use it when the source is an iterator, a generator, a file object, or an infinite stream where normal slicing is not available or would load too much data.

itertools.islice() syntax

itertools.islice(iterable, stop)
itertools.islice(iterable, start, stop[, step])
  • iterable: the source iterable or iterator.
  • start: the first position to return. If omitted, it starts at 0.
  • stop: the position where iteration stops. This value is not included.
  • step: how many positions to skip between returned values. If omitted, it defaults to 1.

Negative values are not supported. Passing a negative start, stop, or a non-positive step raises ValueError.

Get the first N items

When you pass only stop, islice() returns items from the beginning up to that position.

from itertools import islice

letters = iter("ABCDEFG")
print(list(islice(letters, 3)))

Output:

['A', 'B', 'C']

Use start and stop

Pass start and stop to skip early items and then take a limited slice.

from itertools import islice

letters = iter("ABCDEFG")
print(list(islice(letters, 2, 5)))

Output:

['C', 'D', 'E']

Use a step value

The optional step value skips items between returned elements.

from itertools import islice

numbers = range(10)
print(list(islice(numbers, 1, 9, 2)))

Output:

[1, 3, 5, 7]

Limit an infinite iterator

islice() is especially useful with infinite iterators such as itertools.count(). It lets you take a finite prefix safely.

from itertools import count, islice

first_five_even_numbers = islice(count(0, 2), 5)
print(list(first_five_even_numbers))

Output:

[0, 2, 4, 6, 8]

Remember that islice consumes the iterator

If the input is an iterator, consuming the islice() result advances the original iterator. This is different from slicing a list, where the original list remains fully available.

from itertools import islice

iterator = iter([10, 20, 30, 40, 50])
print(list(islice(iterator, 3)))
print(next(iterator))

Output:

[10, 20, 30]
40

The first three values were consumed by islice(), so the next value from the original iterator is 40. islice consumes an iterator lazily; Python next() Function for Iterators explains one-step consumption, defaults at exhaustion, and custom iterator behavior.

Skip a file header or first row

A practical use case is skipping a header row while processing lines. This example uses a small list of strings, but the same pattern works with a real file object.

from itertools import islice

lines = ["header\n", "row1\n", "row2\n", "row3\n"]
for line in islice(lines, 1, None):
    print(line.strip())

Output:

row1
row2
row3

Negative indexes are not supported

Unlike list slicing, islice() does not support negative start, stop, or step values.

from itertools import islice

try:
    list(islice(range(10), -1, 5))
except ValueError as error:
    print(type(error).__name__)

Output:

ValueError

When to convert islice to a list

islice() returns an iterator. Convert it with list() when you need to print it, reuse it multiple times, get its length, or pass it to code that expects a list.

from itertools import islice

chunk = list(islice(range(100), 10))
print(chunk)

If you only need to loop once, keep it lazy:

for item in islice(range(100), 10):
    print(item)

Common mistakes

  • Expecting a list: islice() returns an iterator, so wrap it in list() when needed.
  • Using negative indexes: negative slicing is not supported for islice().
  • Reusing the same result: an islice object is consumed after iteration.
  • Forgetting source consumption: slicing an iterator advances the underlying iterator.
  • Using it for regular lists only: normal list slicing is simpler when your input is already a list.

Related Python guides

Official reference

Conclusion

Use itertools.islice() when you need lazy slicing for an iterator, generator, file object, or infinite stream. It avoids building unnecessary lists, but it also consumes the source iterator as it reads from it.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted