slice - Deepstash
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.

STASHED IN:

2

🐍 Python docs & Stack Overflow

STASHED IN:

0 Comments

MORE IDEAS FROM slice or islice? When to use which

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__().

STASHED IN:

2

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

STASHED IN:

2

Deepstash helps you become inspired, wiser and productive, through bite-sized ideas from the best articles, books and videos out there.

GET THE APP:

RELATED IDEA

Just Read

We all have this one skill we need to use more: we need to read more.

Reading builds up knowledge if done regularly, like compound interest.

5

STASHED IN:

255

Our brain while listening to words

Our brain uses two separate areas to identify the mood and the real meaning of the words. Words are passed to the left temporal lobe of the brain for processing, and intonation is channelled to the right side of the brain, a region more stimulated by music.

STASHED IN:

94

ADAM SAVAGE

“A jack of all trades is a master of none, but oftentimes better than a master of one.”

11

STASHED IN:

31