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.
Learn Python on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours