Skip to main content
deleted 2 characters in body
Source Link
TylerH
  • 21.3k
  • 85
  • 84
  • 122

Using a for loop, how do I access the loop index, from 1 to 5 in this case?

Using a for loop, how do I access the loop index, from 1 to 5 in this case?

Using a for loop, how do I access the loop index, from 1 to 5 in this case?

Using a for loop, how do I access the loop index, from 1 to 5 in this case?

getting a count for zero items
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
count = 0 # in case items is empty and you need it after the loop
for count, item in enumerate(items, start=1):
    print(count, item)
count = 0 # in case items is empty
for count, item in enumerate(items, start=1):   # default is zero
    print(item)

print('there were {0} items printed'.format(count))
for count, item in enumerate(items, start=1):
    print(count, item)
for count, item in enumerate(items, start=1):   # default is zero
    print(item)

print('there were {0} items printed'.format(count))
count = 0 # in case items is empty and you need it after the loop
for count, item in enumerate(items, start=1):
    print(count, item)
count = 0 # in case items is empty
for count, item in enumerate(items, start=1):   # default is zero
    print(item)

print('there were {0} items printed'.format(count))
edited body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
for indexcount, item in enumerate(items, start=1):
    print(indexcount, item)
for index, item in enumerate(items, start=1):
    print(index, item)
for count, item in enumerate(items, start=1):
    print(count, item)
added 78 characters in body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
Rollback to Revision 11
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
The question changed. Copy edited.
Source Link
Peter Mortensen
  • 31.2k
  • 22
  • 110
  • 134
Loading
deleted 3 characters in body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
added 2 characters in body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
added 120 characters in body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
Rollback to Revision 6
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
Is there a reason to quote the question in the answer?
Source Link
Clément
  • 13.1k
  • 16
  • 80
  • 123
Loading
added 406 characters in body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
added 59 characters in body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
added 57 characters in body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
added 564 characters in body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
added 395 characters in body
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading
Source Link
Aaron Hall
  • 400.7k
  • 93
  • 417
  • 342
Loading