Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Vincent Vande Vyvre Newsgroups: comp.lang.python Subject: Re: Exclude every nth element from list? Date: Sat, 26 Mar 2016 18:12:35 +0100 Lines: 24 Message-ID: References: <94237154-22af-4e64-85ee-9b4cf334fa6f@googlegroups.com> <56F6C182.1000106@telenet.be> Reply-To: vincent.vandevyvre@oqapy.eu Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de YTIkDsbuzW5H1ziCcV3Slg7pAHgSUEuTFlXO2WFfjYRg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '"_")': 0.16; "'c',": 0.16; "'d',": 0.16; "'e',": 0.16; "'o',": 0.16; "'r',": 0.16; 'excludes': 0.16; 'from:name:vincent vande vyvre': 0.16; 'received:195.130': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'received:telenet-ops.be': 0.16; '\xe9crit': 0.16; 'element': 0.18; '>>>': 0.20; 'select': 0.23; 'insert': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'correct': 0.28; 'received:be': 0.30; 'list': 0.34; 'something': 0.35; 'there': 0.36; 'to:addr:python- list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'charset:windows-1252': 0.62; 'more': 0.63; 'python-list': 0.66; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'correction,': 0.84 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: <56F6C182.1000106@telenet.be> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105773 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']