Negative array indicies and slice()

Andrew Robinson andrew3 at r3dsolutions.com
Mon Oct 29 00:52:02 EDT 2012


On 10/29/2012 04:32 AM, Chris Angelico wrote:
> I wonder if what the OP is looking for is not slicing, but something 
> more akin to map. Start with a large object and an iterator that 
> produces keys, and create an iterator/list of their corresponding 
> values. Something like: a=[1,2,3,4,5,6,7,8,9,10] b=[a[i] for i in 
> xrange(-4,3)] It's not strictly a slice operation, but it's a similar 
> sort of thing, and it can do the wraparound quite happily. ChrisA 

A list comprehension ?
That does do what I am interested in, *very* much so.  Quite a gem, Chris!

:-\
I am curious as to how quickly it constructs the result compared to a 
slice operation.

Eg:
a[1:5]
vs.
[ a[i] for i in xrange[1:5] ]

But, unless it were grossly slower -- so that if/then logic and slices 
were generally faster -- I will use it.
Thanks.

--Andrew.



More information about the Python-list mailing list