-1

Thank you for clicking. Trying to print a numbered list. Have a for loop for printing list. Ie.

print("COMPOUND:EMBED=okay but not right")
# for num in range(1,6+1):
#   for error in errors_list:
#       num=str(num)
#       print(num + ".", error, end=", ")
# print()

My wanted output is say:

  1. list_item_1
  2. list_item_2
  3. three,..

Given an exact number of 6 elements, the output is instead:

1. list_item_1
1. list_item_2
1. three,..
2. list_item_1
2. list_item_2
2. three,..
3. list_item_1,..list_item_1

Individually, the print is okay. Ie. for item and for range. I've tried embedding the opposite way, list[i] and compounding the two for statements with and. The last of which retrieves: "num is not defined"?

  • the primary answer at http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops has a good workaround. Thank you for the linking. I had forgotten about the count specifics available with while. –  Sep 24 '15 at 11:10

1 Answers1

0

Use enumerate function:

for index, error in enumerate(errors_list):
    print(index + ".", error, end=", ")
Eugene Soldatov
  • 9,755
  • 2
  • 35
  • 43
  • Is it possible to do it with only for loops? –  Sep 24 '15 at 11:03
  • 1
    What makes you think that is not a `for` loop? – Anand S Kumar Sep 24 '15 at 11:03
  • Haven't learned enumerate yet. Just range(). Is it possible to do it with only those two? Saw enumerate as solution here: http://stackoverflow.com/questions/29074749/creating-numbered-list-of-output –  Sep 24 '15 at 11:06
  • yes, you could use it like written above, also you could add parameter start=1 to start enumerate from 1. – Eugene Soldatov Sep 24 '15 at 11:10