Skip to content

Instantly share code, notes, and snippets.

@ugo-nama-kun
Created August 18, 2021 07:12
Show Gist options
  • Save ugo-nama-kun/3a2df191c6b10a200a86cdd3588ebfcb to your computer and use it in GitHub Desktop.
Save ugo-nama-kun/3a2df191c6b10a200a86cdd3588ebfcb to your computer and use it in GitHub Desktop.
Slicable Deque
# From : https://stackoverflow.com/questions/10003143/how-to-slice-a-deque
class sliceable_deque(collections.deque):
def __getitem__(self, index):
try:
return collections.deque.__getitem__(self, index)
except TypeError:
return type(self)(itertools.islice(self, index.start,
index.stop, index.step))
@ugo-nama-kun
Copy link
Author

We cannot do this:

import collections
import itertools

mydeque = collections.deque.deque()

for i in range(10):
  mydeque.append(0)

print(mydeque[:5])

because we get an error as in the link. Then now we can do this

mydeque = sliceable_deque()

for i in range(10):
  mydeque.append(0)

print(mydeque[:5])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment