slice or islice? When to use which - Deepstash
slice or islice? When to use which

slice or islice? When to use which

🐍 Python docs & Stack Overflow

Keep reading for FREE




slice(start, stop, [step])

  • Standard indexing relies on slice()a[start:stop:step] calls a.__getitem__(slice(start, stop, step)).
  • slice() creates a shallow copy of the selected elements from the original list.
  • Whenever you slice a list, the Python interpreter creates a new list and populates it with references to the elements you selected from the original list. Given that a reference needs 8 bytes on 64-bit systems, the overhead of copying references becomes noticeable when working with large lists. For example, slicing 1000 elements allocates almost 8kb worth of references.


10 reads


islice(iterable, stop)

islice(iterable, start, stop, [step])

  • Returns an iterator that produces the selected elements from the iterable. It skips start elements from the iterable, and then produces values until stop is reached, or until the iterator is exhausted. This affects execution speed when it needs to skip lots of elements.
  • It's memory efficient because it doesn't allocate extra memory for the iterated elements, and it can be used to slice both iterables and iterators. Standard indexing (a[start:stop]) doesn't work on iterators because they don't implement __getitem__().


6 reads

slice or islice?

Choosing between slice() and islice() is a trade-off between memory usage and iteration speed. slice() is faster, but requires more memory, whereas islice() allocates a constant amount of memory, but is slower.

Use slice() when:

  • you need fast iteration speed, and you afford the extra memory usage

Use islice() when:

  • you need to slice iterators, or
  • memory efficiency is important to you


8 reads



Alt account of @ocp. I use it to stash ideas about software engineering

Ready for the next level?

Read Like a Pro


Explore the World’s

Best Ideas

200,000+ ideas on pretty much any topic. Created by the smartest people around & well-organized so you can explore at will.

An Idea for Everything

Explore the biggest library of insights. And we've infused it with powerful filtering tools so you can easily find what you need.

Knowledge Library

Powerful Saving & Organizational Tools

Save ideas for later reading, for personalized stashes, or for remembering it later.

# Personal Growth

Take Your Ideas


Organize your ideas & listen on the go. And with Pro, there are no limits.

Listen on the go

Just press play and we take care of the words.

Never worry about spotty connections

No Internet access? No problem. Within the mobile app, all your ideas are available, even when offline.

Get Organized with Stashes

Ideas for your next work project? Quotes that inspire you? Put them in the right place so you never lose them.


2 Million Stashers



5,740 Reviews

App Store



72,690 Reviews

Google Play

Shankul Varada

Best app ever! You heard it right. This app has helped me get back on my quest to get things done while equipping myself with knowledge everyday.

Ashley Anthony

This app is LOADED with RELEVANT, HELPFUL, AND EDUCATIONAL material. It is creatively intellectual, yet minimal enough to not overstimulate and create a learning block. I am exceptionally impressed with this app!

Sean Green

Great interesting short snippets of informative articles. Highly recommended to anyone who loves information and lacks patience.


Don’t look further if you love learning new things. A refreshing concept that provides quick ideas for busy thought leaders.

Ghazala Begum

Even five minutes a day will improve your thinking. I've come across new ideas and learnt to improve existing ways to become more motivated, confident and happier.

Giovanna Scalzone

Brilliant. It feels fresh and encouraging. So many interesting pieces of information that are just enough to absorb and apply. So happy I found this.

Laetitia Berton

I have only been using it for a few days now, but I have found answers to questions I had never consciously formulated, or to problems I face everyday at work or at home. I wish I had found this earlier, highly recommended!

Jamyson Haug

Great for quick bits of information and interesting ideas around whatever topics you are interested in. Visually, it looks great as well.

Read & Learn

20x Faster





Access to 200,000+ ideas

Access to the mobile app

Unlimited idea saving & library

Unlimited history

Unlimited listening to ideas

Downloading & offline access

Personalized recommendations


Claim Your Limited Offer

Get Deepstash Pro


75% OFF | 1-Year Pro Subscription

Claim Offer