How do you get index in ‘for…in’ loop?

To keep track of the indices of list item, use enumerate() function:

list_colors = ['Red','Green','Blue']
for index, color in enumerate(list_colors): 
    print(f"Index: [{index}], Color: {color}")


Index: [0], Color: Red
Index: [1], Color: Green
Index: [2], Color: Blue

Leave a Reply

Your email address will not be published. Required fields are marked *