spliting a list by nth items

Steven Bethard steven.bethard at gmail.com
Thu Sep 23 18:36:46 EDT 2004


Raymond Hettinger writes:
> The enumerate solution has a lot going for it.  It is more flexible and 
> easily accomodates criteria other than every nth item.

Yeah, I'm not so much worried about this, or I wouldn't be using
    items[start::n]
to make the nth_items list in the first place.  (If I was planning to allow 
for other criteria, I'd probably use filter or ifilter instead of the 
slicing...)

> More importantly, it demonstrates an important guiding principle:  multiple 
> list deletions are a code smell indicating that building a new list is a 
> better approach.

This was kinda my feeling -- hence why I posted.  Part of the problem for me 
was that because the two lists were so closely related semantically, I was 
looking for a syntax that was also closely related.  Sometimes this is a good 
intuition to have, sometimes not. ;)

> The runtime behavior of the non_nth_items[start::n] approach is 
> implementation dependent.

Ahh, I didn't know that.  Good thing to know...

Thanks,

Steve




More information about the Python-list mailing list