Skip to main content
added 2 characters in body
Source Link
Ran Turner
  • 18.8k
  • 9
  • 61
  • 60

You can use range(len(some_list)) and then lookup the index like this

xs = [8, 23, 45]
for i in range(len(xs)):
    print("item #{} = {}".format(i + 1, xs[i]))

Or use the Python’s built-in enumerateenumerate function which allows you to loop over a list and retrieve the index and the value of each item in the list

xs = [8, 23, 45]
for idx, val in enumerate(xs, start=1):
    print("item #{} = {}".format(idx, val))

You can use range(len(some_list)) and then lookup the index like this

xs = [8, 23, 45]
for i in range(len(xs)):
    print("item #{} = {}".format(i + 1, xs[i]))

Or use the Python’s built-in enumerate function which allows you to loop over a list and retrieve the index and the value of each item in the list

xs = [8, 23, 45]
for idx, val in enumerate(xs, start=1):
    print("item #{} = {}".format(idx, val))

You can use range(len(some_list)) and then lookup the index like this

xs = [8, 23, 45]
for i in range(len(xs)):
    print("item #{} = {}".format(i + 1, xs[i]))

Or use the Python’s built-in enumerate function which allows you to loop over a list and retrieve the index and the value of each item in the list

xs = [8, 23, 45]
for idx, val in enumerate(xs, start=1):
    print("item #{} = {}".format(idx, val))
Source Link
Ran Turner
  • 18.8k
  • 9
  • 61
  • 60

You can use range(len(some_list)) and then lookup the index like this

xs = [8, 23, 45]
for i in range(len(xs)):
    print("item #{} = {}".format(i + 1, xs[i]))

Or use the Python’s built-in enumerate function which allows you to loop over a list and retrieve the index and the value of each item in the list

xs = [8, 23, 45]
for idx, val in enumerate(xs, start=1):
    print("item #{} = {}".format(idx, val))