spliting a list by nth items
peter at engcorp.com
Thu Sep 23 19:31:31 CEST 2004
Steven Bethard wrote:
> I feel like this has probably been answered before, but I couldn't
> find something quite like it in the archives. Feel free to point me
> somewhere if you know where this has already been answered.
> 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. So far, the best I've
> come up with is:
> non_nth_items = list(items)
> del non_nth_items[::n]
> Is this the right way to do this?
There's probably no "right" way. Your way isn't the way I
would have thought to do it, I think. But it's brilliant. :-)
(Also simple and clear.)
More information about the Python-list