ShopperCove
Menu
All writingBlogTopicsCategoriesAboutRSS
Blog
Categories
Observability & SRE62All categories
Topics
sre111localhost lab102performance70python58javascript47bundle size36benchmarking35node.js35web development21typescript19benchmark16content creation15All topics
About
Loading article…

ShopperCove

Hands-on notes on developer tools, written after the tool has actually been run.

All writingBlogCategoriesTopicsAboutPrivacyRSS

© 2026 ShopperCove

On this page

  1. Intro — what this post promises
  2. Arms
  3. Lab topology
  4. Lead table — head vs mid (width 100)
  5. Mid 10k & single-element
  6. Streaming win
  7. API rule of thumb
  8. Reading it
  9. Skip cost intuition
  10. Files and cursors
  11. Pitfalls
  12. Reproduce
  13. Limits
  14. Takeaway
  1. Blog
  2. /Observability & SRE

islice vs list Slice Windows: Localhost Lab

Hands-on itertools.islice vs list slice window lab: real ops/s taking ranges from sequences, measured on Linux localhost in this hands-on lab for SREs.

1 October 2026·4 min read

Lab
On this page
  1. Intro — what this post promises
  2. Arms
  3. Lab topology
  4. Lead table — head vs mid (width 100)
  5. Mid 10k & single-element
  6. Streaming win
  7. API rule of thumb
  8. Reading it
  9. Skip cost intuition
  10. Files and cursors
  11. Pitfalls
  12. Reproduce
  13. Limits
  14. Takeaway

Intro — what this post promises

Take a window from a large sequence via itertools.islice vs list[start:stop] (plus next(islice) vs index). This lab reports ops/s on Linux localhost.

It is not the itertools micro-loop bake-off (lab 55) and not chain/flatten (lab 67). Focus: windowing / skipping.

Related links:

  • 01shoppercove.comitertools vs python loops localhost lab
  • 02shoppercove.comitertools chain vs flatten localhost lab
  • 03shoppercove.comheapq merge vs sorted localhost lab
  • 04shoppercove.comzlib vs gzip compress localhost lab
  • 05shoppercove.comdifflib vs set ops localhost lab
  • 06shoppercove.comconfigparser vs json localhost lab
  • 07shoppercove.comhtml escape vs manual localhost lab
  • 08shoppercove.commethodcaller vs getattr localhost lab

Lab honesty (1 Oct 2026 IST): Python 3.13.5. Affiliates: 0. Source list length 200000.

Verdict up front: head 100 — list[:100] ~7162375 ops/s vs islice→list ~1217428; mid 100 @ 50k — list slice ~4267167 vs islice ~7607 (skip tax!). Streaming first-100 from count: islice ~1140818 vs materialize-200k-then-slice ~263.


Arms

ArmPattern
data[start:stop]list slice
list(islice(data, …))islice on list
islice consumeno materialize
islice on range / generatorlazy sources
next(islice(...)) vs data[i]single element

Lab topology

n=200_000 list · windows head/mid/tail · batched inner loops · 7 rounds · p50
metric: ops/s = inner / p50_s

Script: lab-evidence/109-islice-vs-list-slice/results/run_lab.py.


Lead table — head vs mid (width 100)

Windowlist sliceislice→list
head [0:100]71623751217428
mid [50k:50.1k]42671677607
tail last 10049823621839

List slice is O(width). islice on a list still walks/skips to start — catastrophic for mid/tail windows.


Mid 10k & single-element

Armops/s
list[50k:60k]54211
islice list→list (10k)5313
list index at 50k31054358
next(islice at 50k)7441

Streaming win

Armops/s
list(islice(count(), 100))1140818
list(range(200k))[:100]263

islice is the right tool for “first N of a stream.” Never build a giant list just to slice the head.


API rule of thumb

islice(iterable, start, stop) always advances the underlying iterator. Sequences implement efficient __getitem__ slices — use them. Reach for islice when you only have an iterable protocol (files, generators, network cursors).


Reading it

  • Have a list and a random window → list[start:stop].
  • Have an iterator / infinite stream → islice.
  • Avoid islice to skip far into a list — use indexing/slicing.
  • next(islice(seq, i, i+1)) is not a substitute for seq[i] on sequences.

Skip cost intuition

islice(seq, start, stop) must consume start items even when seq is a list — it uses the iterator protocol, not __getitem__ ranges. That is why mid/tail windows collapse from millions of ops/s (slice) to thousands (islice) on this box. The API is correct; the access pattern is wrong for random-access sequences.


Files and cursors

For line iterators, DB cursors, and network streams, list slicing is unavailable until you buffer. There islice (or a bounded for-loop with a counter) is the natural “page” tool — exactly where the streaming arm of this lab applies.


Pitfalls

  • Using islice for mid-list windows on concrete lists.
  • Materializing huge ranges to take a head slice.
  • Confusing with chain/loop labs (55/67).
  • Assuming islice is “always lazier/faster” on lists.

Reproduce

python3 lab-evidence/109-islice-vs-list-slice/results/run_lab.py

Evidence: summary.json, summary.txt.

Prefer documenting why a window is taken (UI page, batch boundary, probe sample) so future readers do not “optimize” a streaming islice back into an accidental full materialization.


Limits

One Linux box. Random-access list + synthetic generators. Not memory-mapped file windows.


Takeaway

On lists, slice wins (head ~7162375 ops/s vs islice ~1217428; mid islice drops to ~7607). On streams, islice crushes materialize-then-slice (~1140818 vs ~263 ops/s).

itertools.islicelist slicewindowinglazy iterablepython itertoolslocalhost labsreops/s

Lab evidence

What I found running this

Lab 1 Oct 2026 IST. Python 3.13.5. head100: list_slice 7162375 ops/s; islice 1217428. mid100 islice 7607. stream islice_count 1140818 vs materialize 263. Not lab 55/67. Affiliates: 0. Evidence: lab-evidence/109-islice-vs-list-slice/. Ran the benchmark on Linux localhost and checked head, mid, tail, and streaming windows.

Notes when a lab post goes up

Occasional email for new hands-on reviews. No sequence and no sponsors.

Keep reading

  • html.escape vs Manual Replace: Localhost Lab

    A hands-on localhost lab comparing html.escape with chained str.replace for safe HTML escaping.

    Lab1 Oct 20264 min read

  • difflib vs set Ops Similarity: Localhost Lab

    Hands-on difflib.SequenceMatcher vs set Jaccard token similarity: real ops/s on token lists, measured on Linux localhost in this hands-on lab for SREs.

    Lab1 Oct 20264 min read

  • groupby vs Manual Group: Localhost Lab

    Hands-on itertools.groupby vs manual dict-of-lists: real records/s grouping pre-sorted key runs, measured on Linux localhost today in this lab for SREs.

    Lab1 Oct 20264 min read