The enumerate is a built-in function that returns an enumerate object.
The call is enumerate(sequence, start=0)
The output object includes a counter like so: (0, thing), (1, thing), (2, thing),
and so on.
As input it takes a sequence like a list, tuple or iterator. The start parameter is optional.
If the start parameter is set to one, counting will start from one instead of zero
Related course: Complete Python Bootcamp: Go from zero to hero in Python
Create a sequence and feed it to the enumerate function. This can be any type of sequence, in this example we use a list. Then we output the object.
Try the program below:
# create a sequence
You should see this output:
The returned object can be treated like an iterator: the next method call will work:
browsers = ['Chrome','Firefox','Opera','Vivaldi']
Try the exercises below
- Combine a for loop with an enumerable.