What we usually do if we want to print the content of a list with its index number?
>>> i = 0
>>> for item in items:
… print index, item
… i+= 1
There is actually a short cut to do this. Python has an function called ‘enumerate’. The enumerate function takes a list and returns a (index, item) pair.
>>> print list(enumerate(items))
The reason because we are using a list conversion is, enumerate function is a generator function, which produces one pair at a time. But print cannot be applied to such a function because it print result at once.
As enumerate is a generator function we can apply next() on it.
>>> items = [‘a’, ‘b’, ‘c’, ‘d’]
>>> e= enumerate(items)