Python enumerate() FunctionPython Functions

Difficulty Level:
Difficulty Level:

The enumerate() method adds counter to an iterable and returns it as an enumerate object.

Example

#!/usr/bin/python3

direction = ['East', 'South', 'West', 'North']
print(list(enumerate(direction)))

print(list(enumerate(direction, start = 1)) )
Output:

[(0, 'East'), (1, 'South'), (2, 'West'), (3, 'North')]
[(1, 'East'), (2, 'South'), (3, 'West'), (4, 'North')]

Syntax

enumerate(iterable, start = 0)

Parameters

Name Description
iterable A sequence, an iterator, or objects that support iteration
start A number. Enumerate() starts counting from this number

Return Value

It returns an enumerate object of the given iterable.

Advertisement

© 2021 Pythonexample.org, all rights reserved. Privacy Policy | Contact Us