ScanSkill

enumerate

This enumerate() function returns an enumerated object adding counters to the iterable.

Syntax

enumerate(*iterable, start=0*)

Here,

  • iterable: Required. Must be a sequence, an iterator, or some other object which supports iteration.
  • start: Optional. Index value at which enumeration starts. If start is not used, the default will be 0.

Note:

Examples

  • General uses
>>> # without start value specified
>>> list1 = ['information', 'technology', 'pvt.', 'Ltd.']
>>> enumlist1 = enumerate(list1)
>>> list(enumlist1)
[(0, 'information'), (1, 'technology'), (2, 'pvt.'), (3, 'Ltd.')]

>>> # with start value specified
>>> list1 = ['information', 'technology', 'pvt.', 'Ltd.']
>>> enumlist1 = enumerate(list1, 10)
>>> list(enumlist1)
[(10, 'information'), (11, 'technology'), (12, 'pvt.'), (13, 'Ltd.')]
  • With for loop
>>> list1 = ['information', 'technology', 'pvt.', 'Ltd.']
>>> for i in enumerate(list1):
...     print(i)
... 
(0, 'information')
(1, 'technology')
(2, 'pvt.')
(3, 'Ltd.')

>>> for count, i in enumerate(list1):
...     print(count, i)
... 
0 information
1 technology
2 pvt.
3 Ltd.

>>> for count, i in enumerate(list1, 10):
...     print(count, i)
... 
10 information
11 technology
12 pvt.
13 Ltd.