Python enumerate()
The enumerate() function is a built-in Python function that adds a counter to an iterable and returns it as an enumerate object. It takes an iterable object (like a list, tuple, or string) and returns a sequence of tuples containing indices and corresponding values from the iterable. This function is particularly useful when you need to track both the position and value while looping through sequences.
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 CoursesIncludes 6 Courses
- With Professional CertificationWith Professional Certification
- Beginner Friendly.75 hours75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With CertificateWith Certificate
- Beginner Friendly.24 hours24 hours
Syntax
enumerate(iterable, start=0)
Parameters:
iterable: A sequence, an iterator, or objects that support iterationstart(Optional): The index value from which the counter is to begin. The default value is 0
Return Value:
Returns an enumerate object (iterator) containing tuples with the count (starting from the value of start) and values from the iterable.
Example 1: Enumerating a List
This example demonstrates how the enumerate() function is used on a list:
# Create a simple listfruits =['apple','banana','cherry']# Basic usage of enumerate() in a for loopfor index, fruit inenumerate(fruits):print(f"{index}: {fruit}")# Using enumerate() with a custom start indexfor index, fruit inenumerate(fruits, start=1):print(f"{index}: {fruit}")Copy to clipboardCopy to clipboard
This will print the following output:
0: apple1: banana2: cherry1: apple2: banana3: cherryCopy to clipboardCopy to clipboard
Example 2: Enumerating a Tuple
This example demonstrates how to work with enumerate() on a tuple of colors. It illustrates converting an enumerate object to different data structures and how to use the next() function to access items from an enumerate object sequentially.
colors =('red','green','blue')# Basic enumeration of a tupleenum_colors =enumerate(colors)print(list(enum_colors))# Converting to a dictionaryenum_colors =enumerate(colors)colors_dict =dict(enum_colors)print(colors_dict)# Accessing next item in the enumerate objectenum_colors =enumerate(colors)first_item =next(enum_colors)print(first_item)print(next(enum_colors))Copy to clipboardCopy to clipboard
This will result in the following output:
[(0, 'red'), (1, 'green'), (2, 'blue')]{0: 'red', 1: 'green', 2: 'blue'}(0, 'red')(1, 'green')Copy to clipboardCopy to clipboard
Codebyte Example
The following examples show how the enumerate() function is used with several iterable types:
Visit usVisit usCodeHide codeOutputHide outputCopy to your clipboard
Frequently Asked Questions
All contributors
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 CoursesIncludes 6 Courses
- With Professional CertificationWith Professional Certification
- Beginner Friendly.75 hours75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With CertificateWith Certificate
- Beginner Friendly.24 hours24 hours
