Python Programming - Loops - Discussion

Discussion Forum : Loops - General Questions (Q.No. 5)
5.
What does the enumerate function do when used in a for loop?
Counts the total number of iterations
Iterates over the values of a sequence
Adds an index to each element in an iterable
Reverses the order of iteration
Answer: Option
Explanation:

The enumerate() function adds an index to each element in an iterable, returning pairs of index and element.

my_list = ['apple', 'banana', 'orange', 'grape']

for index, value in enumerate(my_list):
    print(f"Index: {index}, Value: {value}")

Output:

Index: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: orange
Index: 3, Value: grape
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.