spliting a list by nth items

Michael Hoffman m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Thu Sep 23 19:32:40 CEST 2004


Steven Bethard wrote:

> I have a list in a particular order that I want to split into two
> lists: the list of every nth item, and the list of remaining items. 
> It's important to maintain the original order in both lists.
> 
> So the first list is simple:
> 
> nth_items = items[::n]
> 
> The second list is what's giving me trouble.

non_nth_items = [x for x in items if x % n]
--
Michael Hoffman



More information about the Python-list mailing list