enumerate()
Published May 17, 2023
Contribute to Docs
The enumerate()
function returns a list of tuples containing an index and an element for each of the elements in an iterator.
Syntax
enumerate(iterable)
The iterable
parameter can be one of the following:
- A
string
- A
list
,tuple
,dictionary
, orset
. - An iterable object such as one returned by the
iter()
function.
Example
The example below demonstrates how the enumerate()
function is used on a list:
companies_list = ["Google","Microsoft","Amazon"]for index,company in enumerate(companies_list):print(f"{index}. {company}")
This will print the following output:
0. Google1. Microsoft2. Amazon
Codebyte Example
The following examples show how the enumerate()
function is used with several iterable types:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.