Exclude every nth element from list?

beliavsky at aol.com beliavsky at aol.com
Sat Mar 26 18:08:24 EDT 2016


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
> 
> 
> -- 
> Dr. Gary Herron
> Department of Computer Science
> DigiPen Institute of Technology
> (425) 895-4418

Thanks to you and others who replied. I see that enumerate is a useful function.



More information about the Python-list mailing list