Retrieving the index of elements in 'for' loop

Submitted 3 years, 2 months ago
Ticket #335
Views 234
Language/Framework Python
Priority Low
Status Closed

How do I access the index in a for loop, such that the output will be like below...

OUTPUT:

item 1 = 8
item 2 = 23
item 3 = 45
item 4 = 12
item 5 = 78

I want this output by using for loop

Submitted on Feb 03, 21
add a comment

1 Answer

Verified

The problem can be solved by using enumerate() function in the for loop

ints = [8, 23, 45, 12, 78]
for idx, val in enumerate(ints):
    print("item",idx+1,"=", val)

then the output returned will be:

OUTPUT:

item 1 = 8
item 2 = 23
item 3 = 45
item 4 = 12
item 5 = 78

Submitted 3 years, 2 months ago
sai


Latest Blogs