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


Groups > comp.lang.python > #8107

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 <enalicho@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '21,': 0.09; 'pm,': 0.10; '>>>': 0.12; 'wrote:': 0.14; 'billy': 0.16; 'clunky.': 0.16; 'properly.': 0.16; 'forgotten': 0.16; 'received:74.125.82.174': 0.16; 'received:mail-wy0-f174.google.com': 0.16; 'cc:addr:python- list': 0.17; 'tue,': 0.17; 'header:In-Reply-To:1': 0.21; 'seems': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'indices': 0.23; 'message-id:@mail.gmail.com': 0.28; 'subject:?': 0.29; 'cc:addr:python.org': 0.30; 'iterating': 0.30; 'it.': 0.31; 'print': 0.31; 'using': 0.35; 'received:google.com': 0.37; 'two': 0.37; 'think': 0.38; 'received:74.125.82': 0.38; 'received:74.125': 0.38; 'could': 0.38; 'subject:: ': 0.38; 'subject:over': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type:content-transfer-encoding; bh=f55RCgautoHlfySp1MwjPDRs05t0qajsZ85rEDDqjuk=; b=TyF8KJRY/5F2+x4QfuISy9ISqwBi/f558ShEBckdKd0VgUVw3BxQX+t0+i+yuC/rd1 uNY+aK6zpYlkKvIVVi9FrbXljzNyCcNkIexhqhULlsrysasmvfI4WhOEqTt3FussgLdY rEyVTEfG4E9Ang0c5QmDZvdFGz+P738qzNC5k=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=Lg+zlKylh1tKPIrHADd/VG0/ygWxi1VN4yFRwTC1tWDMl8Y+2zdbqxD0+TfllTttAI LXnZ8YKmDsI2750FttspYMmUrMnH7wdGEjw8plG1/uLtHTZ3VjT4JPY3u7mUYrJj8IbO niMBlqvzG/WwXR9xUn5LaHYKbW52vY/yjwnuo=
MIME-Version 1.0
In-Reply-To <itqmkb$h56$1@speranza.aioe.org>
References <itqmkb$h56$1@speranza.aioe.org>
From Noah Hall <enalicho@gmail.com>
Date Tue, 21 Jun 2011 19:19:30 +0100
Subject Re: Better way to iterate over indices?
To Billy Mays <noway@nohow.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
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.233.1308680392.1164.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 82.94.164.166
X-Trace 1308680392 news.xs4all.nl 49042 [::ffff:82.94.164.166]:42708
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8107

Show key headers only | View raw


On Tue, Jun 21, 2011 at 7:05 PM, 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.

You're not using it properly. Think about it. You're giving two names
- i and v. You've forgotten about v -

>>> for i, v in enumerate('fish'):
...     print i, v
...
0 f
1 i
2 s
3 h


HTH.

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