Describe
The enumerate () function is used to combine a data object that can be traversed (such as a list, tuple, or string) into an index sequence, while listing data and data subscripts, typically used in a for loop.
Python 2.3. The above version is available, 2.6 add the start parameter.
Grammar
The following is the syntax for the Enumerate () method:
Enumerate (sequence, [start=0])
Parameters
- Sequence--a sequence, iterator, or other supporting iteration object.
- Start-the subscript start position.
return value
Returns the enumerate (enum) object.
Instance
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))#Small label starting from 1[(1,'Spring'), (2,'Summer'), (3,'Fall'), (4,'Winter')]
Normal. forLoops>>>i =0>>> seq = [' One',' Both','three']>>> forElementinchseq: ...PrintI, Seq[i] ... i+=1... 0 One1 Both2three forcyclic use of enumerate>>>SEQ = [' One',' Both','three']>>> forI, ElementinchEnumerate (seq): ...PrintI, Element ... 0 One1 Both2 Three
Python Enumerate () function----Enumeration