Python Basics Flashcards

1
Q

Give an example of enumerate and explain what it does

A

Enumerate generates tuples that counts (starting from 0), each of the items in a list, or each letter in a string, for example.

The arguments are the object to be enumerated (1) and the number to starting the counting from.

E.g.
# printing the tuples in object directly 
l1 = [101,102,103,104]
for x, ele in enumerate(l1):
    print(x)
Output:
(0, 101)
(1, 102)
(2, 103)
(3, 104)
# changing index and printing separately 
for count,ele in enumerate(l1,100): 
    print (count,ele)
Output:
100 eat
101 sleep
102 repeat

Source: https://www.geeksforgeeks.org/enumerate-in-python/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly