Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105765 > unrolled thread

Exclude every nth element from list?

Started bybeliavsky@aol.com
First post2016-03-26 09:49 -0700
Last post2016-03-29 15:45 +0200
Articles 11 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  Exclude every nth element from list? beliavsky@aol.com - 2016-03-26 09:49 -0700
    Re: Exclude every nth element from list? Gary Herron <gherron@digipen.edu> - 2016-03-26 10:01 -0700
      Re: Exclude every nth element from list? beliavsky@aol.com - 2016-03-26 15:08 -0700
        Re: Exclude every nth element from list? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-26 22:57 +0000
    Re: Exclude every nth element from list? Bob Gailer <bgailer@gmail.com> - 2016-03-26 13:03 -0400
    Re: Exclude every nth element from list? Erik <python@lucidity.plus.com> - 2016-03-26 17:06 +0000
    Re: Exclude every nth element from list? Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2016-03-26 18:06 +0100
    Re: Exclude every nth element from list? Peter Otten <__peter__@web.de> - 2016-03-26 18:06 +0100
    Re: Exclude every nth element from list? Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2016-03-26 18:12 +0100
    Re: Exclude every nth element from list? BartC <bc@freeuk.com> - 2016-03-26 17:50 +0000
    Re: Exclude every nth element from list? "Sven R. Kunze" <srkunze@mail.de> - 2016-03-29 15:45 +0200

#105765 — Exclude every nth element from list?

Frombeliavsky@aol.com
Date2016-03-26 09:49 -0700
SubjectExclude every nth element from list?
Message-ID<94237154-22af-4e64-85ee-9b4cf334fa6f@googlegroups.com>
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? 

[toc] | [next] | [standalone]


#105767

FromGary Herron <gherron@digipen.edu>
Date2016-03-26 10:01 -0700
Message-ID<mailman.43.1459011712.28225.python-list@python.org>
In reply to#105765
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

[toc] | [prev] | [next] | [standalone]


#105785

Frombeliavsky@aol.com
Date2016-03-26 15:08 -0700
Message-ID<eb30d5b5-f8f5-4c51-8b71-2033dc8c65c5@googlegroups.com>
In reply to#105767
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.

[toc] | [prev] | [next] | [standalone]


#105790

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2016-03-26 22:57 +0000
Message-ID<mailman.60.1459033114.28225.python-list@python.org>
In reply to#105785
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

[toc] | [prev] | [next] | [standalone]


#105768

FromBob Gailer <bgailer@gmail.com>
Date2016-03-26 13:03 -0400
Message-ID<mailman.44.1459011801.28225.python-list@python.org>
In reply to#105765
On Mar 26, 2016 12:50 PM, "beliavsky--- via Python-list" <
python-list@python.org> 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?

[y for (i,y) in enumerate(x) if i % n]

[toc] | [prev] | [next] | [standalone]


#105769

FromErik <python@lucidity.plus.com>
Date2016-03-26 17:06 +0000
Message-ID<mailman.45.1459011965.28225.python-list@python.org>
In reply to#105765
On 26/03/16 16:49, 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?

(e for i, e in enumerate(x) if i % n)

K.

[toc] | [prev] | [next] | [standalone]


#105770

FromVincent Vande Vyvre <vincent.vande.vyvre@telenet.be>
Date2016-03-26 18:06 +0100
Message-ID<mailman.46.1459012002.28225.python-list@python.org>
In reply to#105765
Le 26/03/2016 17:49, beliavsky--- via Python-list a écrit :
> 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?
Something like that:

 >>> l = list("lkodjuyhrtgfedcvfg")
 >>> l
['l', 'k', 'o', 'd', 'j', 'u', 'y', 'h', 'r', 't', 'g', 'f', 'e', 'd', 
'c', 'v', 'f', 'g']
 >>> ll = [c for i, c in enumerate(l) if i % 3]
 >>> ll.insert(0, l[0])
 >>> ll
['l', 'k', 'o', 'j', 'u', 'h', 'r', 'g', 'f', 'd', 'c', 'f', 'g']



Vincent

[toc] | [prev] | [next] | [standalone]


#105771

FromPeter Otten <__peter__@web.de>
Date2016-03-26 18:06 +0100
Message-ID<mailman.47.1459012021.28225.python-list@python.org>
In reply to#105765
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?

del x[::n]

;)

[toc] | [prev] | [next] | [standalone]


#105773

FromVincent Vande Vyvre <vincent.vande.vyvre@telenet.be>
Date2016-03-26 18:12 +0100
Message-ID<mailman.48.1459012379.28225.python-list@python.org>
In reply to#105765
Le 26/03/2016 18:06, Vincent Vande Vyvre a écrit :
> Le 26/03/2016 17:49, beliavsky--- via Python-list a écrit :
>> 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?
> Something like that:
>
> >>> l = list("lkodjuyhrtgfedcvfg")
> >>> l
> ['l', 'k', 'o', 'd', 'j', 'u', 'y', 'h', 'r', 't', 'g', 'f', 'e', 'd', 
> 'c', 'v', 'f', 'g']
> >>> ll = [c for i, c in enumerate(l) if i % 3]
> >>> ll.insert(0, l[0])
> >>> ll
> ['l', 'k', 'o', 'j', 'u', 'h', 'r', 'g', 'f', 'd', 'c', 'f', 'g']
>
>
>
> Vincent
Correction, it's more correct with the insert before the list comprehension

 >>> l.insert(0, "_")
 >>> ll = [c for i, c in enumerate(l) if i % 3]
 >>> ll
['l', 'k', 'd', 'j', 'y', 'h', 't', 'g', 'e', 'd', 'v', 'f']

[toc] | [prev] | [next] | [standalone]


#105779

FromBartC <bc@freeuk.com>
Date2016-03-26 17:50 +0000
Message-ID<nd6hul$gie$1@dont-email.me>
In reply to#105765
On 26/03/2016 16:49, beliavsky@aol.com wrote:
> I can use x[::n] to select every nth element of a list.

I would use x[n-1::n] if 'nth' means the 3rd element of 
[10,20,30,40,...] is 30. Otherwise every selection will include the 
first, no matter what n is.

-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#105984

From"Sven R. Kunze" <srkunze@mail.de>
Date2016-03-29 15:45 +0200
Message-ID<mailman.152.1459259115.28225.python-list@python.org>
In reply to#105765
On 26.03.2016 18:06, Peter Otten wrote:
> 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?
> del x[::n]
>
> ;)

Actually quite nice.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web