Exclude every nth element from list?

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Mar 26 18:57:39 EDT 2016


On 26/03/2016 22:08, beliavsky--- via Python-list wrote:
> On Saturday, March 26, 2016 at 1:02:06 PM UTC-4, Gary Herron wrote:
>> On 03/26/2016 09:49 AM, beliavsky--- via Python-list wrote:
>>> I can use x[::n] to select every nth element of a list. Is there a one-liner to get a list that excludes every nth element?
>>
>> Yes:
>>
>>   >>> L=list(range(20))
>>   >>> [x   for i,x in enumerate(L)   if i%3 != 0]
>> [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19]
>>
>>
>> Gary Herron
>>
>
> Thanks to you and others who replied. I see that enumerate is a useful function.
>

It is, but you may like to note that you don't have to count from zero. 
  The 'start' keyword can be set to anything you like.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list