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


Groups > comp.lang.python > #104003

Re: list reversal error

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Gary Herron <gherron@digipen.edu>
Newsgroups comp.lang.python
Subject Re: list reversal error
Date Thu, 3 Mar 2016 15:15:19 -0800
Lines 46
Message-ID <mailman.172.1457047506.20602.python-list@python.org> (permalink)
References <8b3d06eb-0027-4396-bdf8-fee0cc9ff771@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de xkRnFMsES+J3ISdBTxn2gQa5gRLUysumy8mREfLuhsiA==
Return-Path <gherron@digipen.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.068
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; 'error:': 0.05; 'iterate': 0.09; 'wastes': 0.09; 'python': 0.10; 'subject:error': 0.11; 'index': 0.13; '(say': 0.16; 'example)': 0.16; 'indexerror:': 0.16; 'len(data)': 0.16; 'loops': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reversing': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'produces': 0.22; 'trying': 0.22; 'elements': 0.23; 'header :In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'values': 0.28; 'datum': 0.29; 'skip:d 20': 0.34; 'list': 0.34; 'but': 0.36; 'list,': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'really': 0.37; 'thanks': 0.37; 'doing': 0.38; '(with': 0.38; 'whatever': 0.39; 'build': 0.40; 'to:addr:python.org': 0.40; 'your': 0.60; "you'll": 0.61; 'email addr:gmail.com': 0.62; 'back': 0.62; 'charset:windows-1252': 0.62; 'more': 0.63; 'reverse': 0.66; 'dr.': 0.69; 'received:204': 0.75; 'received:10.10': 0.76; 'institute': 0.77; '(425)': 0.84; '895-4418': 0.84; 'digipen': 0.84; 'front.': 0.84; 'herron': 0.84; 'order:': 0.84
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1
In-Reply-To <8b3d06eb-0027-4396-bdf8-fee0cc9ff771@googlegroups.com>
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:104003

Show key headers only | View raw


On 03/03/2016 02:51 PM, vlyamtsev@gmail.com wrote:
> 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

Look at the values (say with a print) you get from your line
     j = len(data) - i
You'll find that that produces (with a list of 4 elements for example) 
4,3,2,1 when in fact you want 3,2,1,0. Soo you really want
     j = len(data) - i -1


Better yet, use more of Python with
     data1 = list(reversed(data))

Or don't even make a new list, just reverse the original list in place
 >>> L=[1,2,3]
 >>> L.reverse()
 >>> L
[3, 2, 1]

Or even better, if you simply want to iterate through the original list, 
but in reverse order:
     for datum in reversed(data):
         ... whatever with datum ...
which wastes no time actually reversing the list, but simply loops 
through them back to front.


Gary Herron

-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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