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))

 

Need Help With Python Development?

Work with our skilled Python developers to accelerate your project and boost its performance.

Hire Python Developers

Support On Demand!

Related Q&A