Search code examples
pythonlistfor-loopembedding

Python:embedded for loops: numbering list output


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"?


Solution

  • Use enumerate function:

    for index, error in enumerate(errors_list):
        print(index + ".", error, end=", ")