c[:]()

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Jun 5 22:42:47 EDT 2007


On Tue, 05 Jun 2007 18:26:50 -0400, Terry Reedy wrote:

> "Warren Stringer" <warren at muse.com> wrote in message
> news:015701c7a7a4$584fac00$240110ac at Muse... | I am porting code that
> only uses this form |    a[:4]b()
> |
> | Which translates to:
> |
> | for i in range(4):
> |    a[i].b()
> 
> Or, more directly and perhaps more efficiently:
> 
> for e in a[:4]: e.b()


I'm thinking that is more efficient only because four is a small number. 
If you wanted to iterate over the first 2**22 items in a list of 2**22+1 
items, making a copy of the sub-list first would probably be a bad idea :)


Is there a general Pythonic idiom for efficiently walking over part of a 
sequence without copying it first? Should there be?


-- 
Steven.



More information about the Python-list mailing list