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: Complete Python Bootcamp: Go from zero to hero in Python

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
browsers = ['Chrome','Firefox','Opera','Vivaldi']

# create an enumeratable and convert to list
x = list(enumerate(browsers))
print(x)

You should see this output:

enumerate

The returned object can be treated like an iterator: the next method call will work:

browsers = ['Chrome','Firefox','Opera','Vivaldi']
eObj = enumerate(browsers)

x = next(eObj)
print(x)
x = next(eObj)
print(x)

Exercise

Try the exercises below

  1. Combine a for loop with an enumerable.

Download answers