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


Groups > comp.lang.python > #104000

Re: list reversal error

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Joel Goldstick <joel.goldstick@gmail.com>
Newsgroups comp.lang.python
Subject Re: list reversal error
Date Thu, 3 Mar 2016 18:13:59 -0500
Lines 57
Message-ID <mailman.169.1457046848.20602.python-list@python.org> (permalink)
References <8b3d06eb-0027-4396-bdf8-fee0cc9ff771@googlegroups.com> <nbag4v$vs$1@reader1.panix.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de wutOnRUkbciPTgJeEbLx4Q+rpMZTz290O4F+Esr7Qr9w==
Return-Path <joel.goldstick@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'error:': 0.05; '&quot;the': 0.07; 'edward': 0.09; 'indexes': 0.09; 'python': 0.10; 'subject:error': 0.11; 'index': 0.13; '&gt;&gt;&gt;': 0.15; 'thu,': 0.15; '2016': 0.16; 'evaluates': 0.16; 'indexerror:': 0.16; 'len(data)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'element': 0.18; '&gt;': 0.18; '>>>': 0.20; 'to:name:python- list@python.org': 0.20; 'trying': 0.22; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'message-id:@mail.gmail.com': 0.27; 'yield': 0.27; 'actual': 0.28; 'loop,': 0.29; 'url:mailman': 0.30; '"the': 0.32; 'url:python': 0.33; 'url:listinfo': 0.34; 'skip:d 20': 0.34; 'lists': 0.34; 'list': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'skip:& 60': 0.35; 'but': 0.36; 'list,': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'thanks': 0.37; 'doing': 0.38; 'skip:z 10': 0.38; 'received:209': 0.38; 'url:mail': 0.40; 'build': 0.40; 'to:addr:python.org': 0.40; 'your': 0.60; 'john': 0.61; 'email addr:gmail.com': 0.62; 'mar': 0.65; 'reverse': 0.66; 'url:info': 0.71; 'email addr:panix.com': 0.84; 'bears': 0.91; 'joel': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=RQmsTP3TXLcrG5NOeKeQdMMRdMR67GYlM9we+B0NZa4=; b=Sm/4ibqjHMajSJ0QMGpPEYpJcSlIS33uxGXYroq7aGIzwRpRHZnIkoiPmPP4Q4onzm bqjihNY0HDxpTmB34Ty7bW/GAfd5IK7BkzAmsjc9RL1fp+sCZrsCnOX0dwbavaaLYnOH ar92cvYT7+bqWVol/GYV3AD8oBGJUTrkEYwQRLyZRJfu5umYm47e5ZCTnv996tfBnbv2 sUa3sVIfAHCaoMYOBAKSZNVFUm+jI3RKJljAPj6I+Z1GhlRlGa7Td+8VkdNIdKp9wcg0 SMB/IbQtvD+pDmg6rwBGFfyBbhMBcdJldOOVG2d/+eBdl222Mzx/4fIbfBW1jZETvvLA U6+g==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=RQmsTP3TXLcrG5NOeKeQdMMRdMR67GYlM9we+B0NZa4=; b=IHElbAwqgH1tXfmMLRX2egKHoKIUjaBmdqd+X1OgG5kD4kB5T9tPviiF1TMT1tfBj4 2fM2wM9pC/mNgxT6lVDifJ9q2sgWiT14ZprW3rftJxMPTohm67Ib/h7FtA5/GJWvjfdl 1AcJFp7oXH2nztgn2Lbn/GL9Y2UCkNRbGEFrodwo2Xbqo7/J6LZ91VFhajRGDNNnX7dz SmlJ8k+lqCMKLWrrdRDyYsClAvPHG5cwU59tEiFRvK47R9eYaOAS5nHx86kEZU0d+X83 QtQat7C9eK/QYOSkBrTFL3w18/kOxqkOXzFs4nHmxUlpSg4dlJBA+V2qepzt/SBCE2N4 5jow==
X-Gm-Message-State AD7BkJJ4pK1omrgz7rexxmyUBZUiEhApIEmZxySYrJ/G41JAauZo6ycJV0AtKF3OO9l54hv87VvvOGyLjtJZ3A==
X-Received by 10.107.62.195 with SMTP id l186mr5493521ioa.180.1457046840024; Thu, 03 Mar 2016 15:14:00 -0800 (PST)
In-Reply-To <nbag4v$vs$1@reader1.panix.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.21
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:104000

Show key headers only | View raw


On Thu, Mar 3, 2016 at 6:08 PM, John Gordon <gordon@panix.com> wrote:

> In <8b3d06eb-0027-4396-bdf8-fee0cc9ff771@googlegroups.com>
> vlyamtsev@gmail.com writes:
>
> > i have list of strings "data" and i am trying to build reverse list data1
> > data1 = []
> > for i in range(len(data)):
> >    j = len(data) - i
> >    data1.append(data[j])
>
> > but i have the following error:
> > data1.append(data[j])
> > IndexError: list index out of range
> >
> > am i doing it wrong?
> > Thanks
>
> Python lists are zero-indexed, meaning a list of five items will have
> indexes 0 to 4.
>
> The first time through your loop, i is 0, so
>
>     j = len(data) - i
>
> evaluates to
>
>     j = len(data)
>
> which would yield 5 for a five-element list, but the last actual element
> is in data[4].
>

>>> s = "123"
>>> s2 = s[::-1]
>>> s2
'321'
>>>

Use reverse slice

>
> --
> John Gordon                   A is for Amy, who fell down the stairs
> gordon@panix.com              B is for Basil, assaulted by bears
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com/ <http://joelgoldstick.com/stats/birthdays>
http://cc-baseballstats.info/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

list reversal error vlyamtsev@gmail.com - 2016-03-03 14:51 -0800
  Re: list reversal error John Gordon <gordon@panix.com> - 2016-03-03 23:08 +0000
    Re: list reversal error Joel Goldstick <joel.goldstick@gmail.com> - 2016-03-03 18:13 -0500
    Re: list reversal error MRAB <python@mrabarnett.plus.com> - 2016-03-03 23:20 +0000
  Re: list reversal error Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-03 23:20 +0000
  Re: list reversal error Gary Herron <gherron@digipen.edu> - 2016-03-03 15:15 -0800
  Re: list reversal error Steven D'Aprano <steve@pearwood.info> - 2016-03-04 11:06 +1100

csiph-web