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


Groups > comp.lang.python > #8108

Re: Better way to iterate over indices?

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 <bsk16@case.edu>
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 <itqmkb$h56$1@speranza.aioe.org>
References <itqmkb$h56$1@speranza.aioe.org>
Date Tue, 21 Jun 2011 11:20:41 -0700
Subject Re: Better way to iterate over indices?
From Benjamin Kaplan <benjamin.kaplan@case.edu>
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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.234.1308680502.1164.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Tue, Jun 21, 2011 at 11:05 AM, Billy Mays <noway@nohow.com> wrote:
> I have always found that iterating over the indices of a list/tuple is not
> very clean:
>
> for i in range(len(myList)):
>    doStuff(i, myList[i])
>
>
>
>
> I know I could use enumerate:
>
> for i, v in enumerate(myList):
>    doStuff(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
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Better way to iterate over indices? Billy Mays <noway@nohow.com> - 2011-06-21 14:05 -0400
  Re: Better way to iterate over indices? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-21 12:15 -0600
  Re: Better way to iterate over indices? Noah Hall <enalicho@gmail.com> - 2011-06-21 19:19 +0100
  Re: Better way to iterate over indices? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-06-21 11:20 -0700
  Re: Better way to iterate over indices? Ethan Furman <ethan@stoneleaf.us> - 2011-06-21 11:35 -0700

csiph-web