Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.019 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '21,': 0.09; 'iterate': 0.09; 'there?': 0.09; 'am,': 0.14; 'wrote:': 0.14; '--bill': 0.16; 'billy': 0.16; 'clunky.': 0.16; 'indices.': 0.16; 'tue,': 0.17; 'header:In-Reply-To:1': 0.21; 'seems': 0.21; 'indices': 0.23; 'url:mailman': 0.26; 'message-id:@mail.gmail.com': 0.28; 'subject:?': 0.29; 'instead': 0.29; 'received:209.85.160': 0.29; 'bit': 0.30; 'url:listinfo': 0.30; 'fact': 0.30; 'iterating': 0.30; 'syntax,': 0.30; 'seem': 0.32; 'cases': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'list': 0.33; 'actually': 0.33; 'there': 0.35; 'typical': 0.35; 'probably': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'ways': 0.37; 'case': 0.37; 'url:python': 0.38; 'could': 0.38; 'url:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; 'huge': 0.62; 'subject:over': 0.84 MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 21 Jun 2011 11:20:41 -0700 Subject: Re: Better way to iterate over indices? From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Junkmail-Status: score=10/49, host=mpv2.tis.cwru.edu X-Junkmail-Signature-Raw: score=unknown, refid=str=0001.0A020206.4E00E12E.00FE,ss=1,fgs=0, ip=209.85.160.54, so=2010-12-23 16:51:53, dmn=2009-09-10 00:05:08, mode=single engine X-Junkmail-IWF: false X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 33 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308680502 news.xs4all.nl 49045 [::ffff:82.94.164.166]:49449 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8108 On Tue, Jun 21, 2011 at 11:05 AM, Billy Mays wrote: > I have always found that iterating over the indices of a list/tuple is no= t > very clean: > > for i in range(len(myList)): > =A0 =A0doStuff(i, myList[i]) > > > > > I know I could use enumerate: > > for i, v in enumerate(myList): > =A0 =A0doStuff(i, myList[i]) > > ...but that stiff seems clunky. Why does enumerate seem clunky, other than the fact that you should probably have doStuff(i,v) instead of myList[i] in there? It's a bit more awkward than C's syntax, but since the typical use case is just iteration anyway, it's not a huge deal for those few cases where you actually need the indices. > > Are there any better ways to iterate over the indices of a list /tuple? > > --Bill > -- > http://mail.python.org/mailman/listinfo/python-list >