Better way to iterate over indices?

Discussion in 'Python' started by Billy Mays, Jun 21, 2011.

  1. Billy Mays

    Billy Mays Guest

    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 know I could use enumerate:

    for i, v in enumerate(myList):
    doStuff(i, myList)

    ....but that stiff seems clunky.

    Are there any better ways to iterate over the indices of a list /tuple?

    --Bill
     
    Billy Mays, Jun 21, 2011
    #1
    1. Advertising

  2. Billy Mays

    Ian Kelly Guest

    On Tue, Jun 21, 2011 at 12:05 PM, Billy Mays <> wrote:
    > I know I could use enumerate:
    >
    > for i, v in enumerate(myList):
    >    doStuff(i, myList)
    >
    > ...but that stiff seems clunky.


    Why not:

    for i, v in enumerate(myList):
    doStuff(i, v)
     
    Ian Kelly, Jun 21, 2011
    #2
    1. Advertising

  3. Billy Mays

    Noah Hall Guest

    On Tue, Jun 21, 2011 at 7:05 PM, 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 know I could use enumerate:
    >
    > for i, v in enumerate(myList):
    >    doStuff(i, myList)
    >
    > ...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.
     
    Noah Hall, Jun 21, 2011
    #3
  4. 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 not
    > very clean:
    >
    > for i in range(len(myList)):
    >    doStuff(i, myList)
    >
    >
    >
    >
    > I know I could use enumerate:
    >
    > for i, v in enumerate(myList):
    >    doStuff(i, myList)
    >
    > ...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 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
    >
     
    Benjamin Kaplan, Jun 21, 2011
    #4
  5. Billy Mays

    Ethan Furman Guest

    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)


    Definitely not beautiful. ;)

    > I know I could use enumerate:
    >
    > for i, v in enumerate(myList):
    > doStuff(i, myList)


    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~
     
    Ethan Furman, Jun 21, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,128
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,479
    runescience
    Feb 9, 2006
  3. Efrat Regev
    Replies:
    2
    Views:
    353
    Efrat Regev
    May 22, 2007
  4. John
    Replies:
    4
    Views:
    944
    RedGrittyBrick
    Apr 1, 2008
  5. Peter v. N.

    Iterate over md Array (the Ruby way)

    Peter v. N., Oct 13, 2005, in forum: Ruby
    Replies:
    10
    Views:
    229
    Gavin Kistner
    Oct 29, 2005
Loading...

Share This Page