Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'elements.': 0.07; 'error:': 0.07; 'string': 0.09; 'iterate': 0.09; 'learn,': 0.09; 'line:': 0.09; 'python': 0.11; 'assignments.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'honesty': 0.16; 'increment': 0.16; 'range.': 0.16; 'elements': 0.16; 'index': 0.16; 'sat,': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'trying': 0.19; 'file,': 0.19; 'print': 0.22; 'header:In-Reply- To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'getting': 0.31; '(since': 0.31; 'file': 0.32; 'probably': 0.32; 'figure': 0.32; 'open': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'hi,': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'problems.': 0.60; 'tell': 0.60; "you're": 0.61; "you'll": 0.62; 'worth': 0.66; 'below:': 0.68; 'apart': 0.72; 'yourself': 0.78; 'homework': 0.84; '2013': 0.98 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 :content-type; bh=vpVxll1E2krMf8TfRIcXtyspE/f+zPqsPmxaHiMR+38=; b=StvXXA9KwDx1ALJ4eYlgkW/KcVyTHV2RBxte93CGaBccREJqjv5JiKuwKmWb17M4Pt dwztevWSoMr4bXRco9r2pVeComHtfkPCn7Dvr/YbuAWeE09CZJBHkvRh+X3K5vS+YSbN IBXLxHRQFunMb1ojw1c24vll5rcawNcMrrU3Adcu8kHHwjOUX+ZoAH3B7VTsN+cs0lfN ytx9YZpBUtB1iJT5dM1VuNH5CnFUrZ5A35uFFleVwP6bRCk43pSkDhkrUUwbDbfhfHdQ Le/pWNDKDPhDrVC2spKzzQpO1Phjd+fpMReJhMUV1QheE3T04xIW0w7A7QATUQ3fB7BN DV8w== MIME-Version: 1.0 X-Received: by 10.52.93.106 with SMTP id ct10mr5376786vdb.83.1372470831258; Fri, 28 Jun 2013 18:53:51 -0700 (PDT) In-Reply-To: <8b12635e-c3e9-49b2-b3fa-792559088821@googlegroups.com> References: <8b12635e-c3e9-49b2-b3fa-792559088821@googlegroups.com> Date: Sat, 29 Jun 2013 11:53:51 +1000 Subject: Re: indexerror: list index out of range?? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372470834 news.xs4all.nl 15901 [2001:888:2000:d::a6]:58055 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49398 On Sat, Jun 29, 2013 at 11:20 AM, Titiksha Joshi wrote: > Hi, > I am working on the following code but am getting the error: list index out of range. I surfed through the group but somehow I am not able to fix my error.Please guide.Structure is given below: > m is a list of 5 elements. I have to match elements of m from fields in file ALL_BUSES_FINAL.cvs. > m=['631138', '601034', '2834', '2908', '64808'] > > i=0 > while i print(i) > my_file = open("ALL_BUSES_FINAL.csv", "r+") > for line in my_file: > if m[i] in line: > print (line) > a.append(line) > i+=1 > print(a) > my_file.close() What this does is open the file once for every string in 'm', and read through it. But you increment i for every line in the file, that's why you're having problems. I think you probably want to open the file once and iterate over it, but you'll need to figure out what you're trying to do before you can figure out how to do it in Python :) Also, be up-front about homework assignments. Honesty won't hurt you (since we can all tell anyway), and it means we know you're trying to learn, not to cheat. And yes, there are a discouraging number of people who do try to cheat, so setting yourself apart from them is well worth it. :) Chris Angelico