The enumerate() function is used to combine a traversable data object (such as a list, tuple or string) into an index sequence, and list data and data subscripts at the same time. It is generally used in a for loop.
Python 2.3. and above are available, and 2.6 adds the start parameter.
The following is the syntax of the enumerate() method:
enumerate(sequence,[start=0])
Return an enumerate (enumeration) object.
The following shows an example of using the enumerate() method:
>>> seasons =['Spring','Summer','Fall','Winter']>>>list(enumerate(seasons))[(0,'Spring'),(1,'Summer'),(2,'Fall'),(3,'Winter')]>>>list(enumerate(seasons, start=1)) #Subscripts start from 1
[(1,' Spring'),(2,'Summer'),(3,'Fall'),(4,'Winter')]
>>> i =0>>> seq =['one','two','three']>>>for element in seq:... print i, seq[i]... i +=10 one 1 two 2 three
>>> seq =['one','two','three']>>>for i, element inenumerate(seq):... print i, element
0 one 1 two 2 three
Recommended Posts