slice - Deepstash

Explore the World's Best Ideas

Join today and uncover 100+ curated journeys from 50+ topics. Unlock access to our mobile app with extensive features.

slice

slice

slice(stop)

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.

2

11 reads

MORE IDEAS ON THIS

itertools.islice

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, ...

2

7 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 constan...

2

8 reads

CURATED FROM

IDEAS CURATED BY

ocpodariu

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

🐍 Python docs & Stack Overflow

Read & Learn

20x Faster

without
deepstash

with
deepstash

with

deepstash

Personalized microlearning

100+ Learning Journeys

Access to 200,000+ ideas

Access to the mobile app

Unlimited idea saving

Unlimited history

Unlimited listening to ideas

Downloading & offline access

Supercharge your mind with one idea per day

Enter your email and spend 1 minute every day to learn something new.

Email

I agree to receive email updates