spliting a list by nth items

Larry Bates lbates at swamisoft.com
Thu Sep 23 14:23:49 EDT 2004


I believe you meant somthing like:

non_nth_items = [items[i-1] for i in range(1,len(items)-1) if i % n]

I don't think the "if x % n" won't work on his list of items

Larry Bates

"Michael Hoffman" <m.h.3.9.1.without.dots.at.cam.ac.uk at example.com> wrote in 
message news:civ1bq$4qp$1 at pegasus.csx.cam.ac.uk...
> 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