Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Gary Herron Newsgroups: comp.lang.python Subject: Re: list reversal error Date: Thu, 3 Mar 2016 15:15:19 -0800 Lines: 46 Message-ID: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104003 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