You can use the enumerate function in Python to iterate over a sequence and simultaneously get the index and the value. Here’s how you can modify your code to achieve the desired output:

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

 

Support On Demand!

Python