Negative array indicies and slice()

Chris Angelico rosuav at gmail.com
Mon Oct 29 08:40:53 EDT 2012


On Mon, Oct 29, 2012 at 3:52 PM, Andrew Robinson
<andrew3 at r3dsolutions.com> wrote:
> 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] ]

For the most part, don't concern yourself with performance. Go with
functionality and readability. In the trivial case shown here, the
slice is WAY clearer, so it should definitely be the one used; in
other cases, the slice might simply be insufficient, so you go with
whatever achieves your goal. Performance will usually be "good
enough", even if there's a marginally faster way.

ChrisA



More information about the Python-list mailing list