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[0]), (1, thing[1]), (2, thing[2]),
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: Python Crash Course: Master Python Programming!
Enumerate object
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'] |
Exercise
Try the exercises below
- Combine a for loop with an enumerable.