Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Joel Goldstick Newsgroups: comp.lang.python Subject: Re: list reversal error Date: Thu, 3 Mar 2016 18:13:59 -0500 Lines: 57 Message-ID: References: <8b3d06eb-0027-4396-bdf8-fee0cc9ff771@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de wutOnRUkbciPTgJeEbLx4Q+rpMZTz290O4F+Esr7Qr9w== Return-Path: 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; '"the': 0.07; 'edward': 0.09; 'indexes': 0.09; 'python': 0.10; 'subject:error': 0.11; 'index': 0.13; '>>>': 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; '>': 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104000 On Thu, Mar 3, 2016 at 6:08 PM, John Gordon 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://cc-baseballstats.info/