Accessing the index in 'for' loops

asked16 years ago
last updated3 years ago
viewed3.8m times
Up Vote5.1kDown Vote

How do I access the index while iterating over a sequence with a for loop?

xs = [8, 23, 45]

for x in xs:
    print("item #{} = {}".format(index, x))

Desired output:

item #1 = 8
item #2 = 23
item #3 = 45