4 Replies - 733 Views - Last Post: 23 February 2013 - 01:24 PM Rate Topic: -----

#1 Orochimaru  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 26-December 12

Is it bad to use i too much in "for i in list", when there are

Posted 23 February 2013 - 01:04 PM

I'm following this tutorial and experimenting with the code so that all the for loops uses i.
http://learnpythonth.../book/ex32.html


Is it bad to use i too much in "for i in list", when there are several for loops in the same script file?

Example code: ex32.py
the_count = [1, 2, 3, 4, 5, 10, 11, 13, 100]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list
for i in the_count:
    print "This is count %3d  number" % i

print "\n\ti = %d\n" % i

# same as above
for i in fruits:
    print "A fruit of type: %s" % i

print "\n\ti = %s\n" % i

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print "I got %r" % i

print "\n\ti = %r\n" % i

# we can also build lists, first start with an empty one
elements = []
elements2 = range(0, 6)

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print "Adding %d to the list." % i
    # append is a function that lists understand
    elements.append(i)

print

# now we can print them out too
for i in elements:
    print "Element was: %d" % i

print

# now we can print them out too
for i in elements2:
    print "Element-2 was: %d" % i



Is This A Good Question/Topic? 0
  • +

Replies To: Is it bad to use i too much in "for i in list", when there are

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,788
  • Joined: 12-December 12

Re: Is it bad to use i too much in "for i in list", when there are

Posted 23 February 2013 - 01:09 PM

No, this is not a problem, as the loop re-initialises the variable each time. It would be if the loops were nested. It does lack a little imagination though:

for fruit in fruits:

Was This Post Helpful? 1
  • +
  • -

#3 Orochimaru  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 26-December 12

Re: Is it bad to use i too much in "for i in list", when there are

Posted 23 February 2013 - 01:16 PM

for i in Thanks:

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,788
  • Joined: 12-December 12

Re: Is it bad to use i too much in "for i in list", when there are

Posted 23 February 2013 - 01:17 PM

View PostOrochimaru, on 23 February 2013 - 01:16 PM, said:

for i in Thanks:

Shouldn't that be:

for q in thanks:

:)
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Is it bad to use i too much in "for i in list", when there are

Posted 23 February 2013 - 01:24 PM

I don't think it's a good idea readability-wise to use the name i for something that is not an index.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1