freeCodeCamp/guide/english/python/enumerate/index.md

900 B
Raw Blame History

title
Python's Enumerate Function

Overview

enumerate() is a built-in function in Python. It is useful for iteration over a list or any other iterable.

Arguments

enumerate() requires an iterable, and start as an optional parameter (it defines where the indexing starts).

Return Value

Returns an enumerate object. If you call list() on it, you can see that each item in the object is a tuple an index followed by the item in the iterable.

Usage Example

animals = ['cat', 'dog', 'rabbit', 'fox', 'wolf'] # A list of animals.
for index, item in enumerate(animals, start=20):
  print(index, item)

Output

20 cat
21 dog
22 rabbit
23 fox
24 wolf

Try it out!

More Information