Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8103 > unrolled thread
| Started by | Billy Mays <noway@nohow.com> |
|---|---|
| First post | 2011-06-21 14:05 -0400 |
| Last post | 2011-06-21 11:35 -0700 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Billy Mays <noway@nohow.com> |
|---|---|
| Date | 2011-06-21 14:05 -0400 |
| Subject | Better way to iterate over indices? |
| Message-ID | <itqmkb$h56$1@speranza.aioe.org> |
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.
Are there any better ways to iterate over the indices of a list /tuple?
--Bill
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-06-21 12:15 -0600 |
| Message-ID | <mailman.231.1308680156.1164.python-list@python.org> |
| In reply to | #8103 |
On Tue, Jun 21, 2011 at 12:05 PM, Billy Mays <noway@nohow.com> wrote:
> I know I could use enumerate:
>
> for i, v in enumerate(myList):
> doStuff(i, myList[i])
>
> ...but that stiff seems clunky.
Why not:
for i, v in enumerate(myList):
doStuff(i, v)
[toc] | [prev] | [next] | [standalone]
| From | Noah Hall <enalicho@gmail.com> |
|---|---|
| Date | 2011-06-21 19:19 +0100 |
| Message-ID | <mailman.233.1308680392.1164.python-list@python.org> |
| In reply to | #8103 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Benjamin Kaplan <benjamin.kaplan@case.edu> |
|---|---|
| Date | 2011-06-21 11:20 -0700 |
| Message-ID | <mailman.234.1308680502.1164.python-list@python.org> |
| In reply to | #8103 |
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
>
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2011-06-21 11:35 -0700 |
| Message-ID | <mailman.235.1308680515.1164.python-list@python.org> |
| In reply to | #8103 |
Billy Mays 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])
Definitely not beautiful. ;)
> I know I could use enumerate:
>
> for i, v in enumerate(myList):
> doStuff(i, myList[i])
If you actually need the index, then this is the way to do it. Note
that in most cases, you don't need the index and can iterate directly:
for v in myList:
doStuff(v)
From your sample code (assuming you don't need i) this does the same thing.
~Ethan~
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web