deque slicing

R

Raymond Hettinger

Anyone know why deques don't support slicing?

In part, it is because of the vague notion that slicing is not what deques are
all about. While it's easy to add a getslice operation, setslice and delslice
require rearranging data outside the slice range. To me, that is a strong
indicator that an application needs a list instead of a deque.

Even getitem/setitem were a stretch (they perform best at endpoints) and were
added to make deques substitutable for lists in stack/queue applications using
d[0] and d[-1] to implement peekleft() and peekright() operations.

Deques emulate some but not all of the list API. The parts that match were done
to make deques easier to learn and to make them more substitutable for lists in
stack/queue apps. The rest of the API was excluded because the list structure
was more appropriate for those operations.


Raymond
 
S

Steven Bethard

Raymond Hettinger said:
Even getitem/setitem were a stretch (they perform best at endpoints) and were
added to make deques substitutable for lists in stack/queue applications
using d[0] and d[-1] to implement peekleft() and peekright() operations.

Ahh, ok, so the functionality I was really looking for is peekleft(n). That
is, a peek function that would return the first n items, starting at the
left. (If you're curious why I wanted this, see the peekable class in
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373.) I assumed
that the best way to get this was slicing, but are there plans for such a
peekleft function?

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top