Efficient way to break up a list into two pieces

MRAB python at mrabarnett.plus.com
Sun Feb 21 20:29:22 CET 2010


MRAB wrote:
> Steven D'Aprano wrote:
> [snip]
>> I'm sympathetic to your concern: I've often felt offended that doing 
>> something like this:
>>
>> x = SomeReallyBigListOrString
>> for item in x[1:]:
>>     process(item)
>>
>> has to copy the entire list or string (less the first item). But 
>> honestly, I've never found a situation where it actually mattered.
>>
> [snip]
> Could lists gain an .items() method with start and end positions? I was
> thinking that it would be a 'view', like with dicts in Python 3. Of
> course, that would only be worthwhile with very long lists:
> 
> x = SomeReallyBigListOrString
> for item in x.items(1):
>     process(item)

Actually, my example is a bit wrong! My suggestion should have said that
the arguments of list.items would resemble those of range:

x = SomeReallyBigListOrString
for item in x.items(1, None): # [1 : None], or just [1 : ]
     process(item)

Having a third 'stride' argument would also be possible.



More information about the Python-list mailing list