Enumerate() in Python - GeeksforGeeks (2024)

Last Updated : 19 Jun, 2024

Improve

Often, when dealing with iterators, we also need to keep a count of iterations. Python eases the programmers’ task by providing a built-in function enumerate() for this task. The enumerate () method adds a counter to an iterable and returns it in the form of an enumerating object. This enumerated object can then be used directly for loops or converted into a list of tuples using the list() function.

Syntax:enumerate(iterable, start=0)

Parameters:

  • Iterable: any object that supports iteration
  • Start: the index value from which the counter is to be started, by default it is 0

Return: Returns an iterator with index and element pairs from the original iterable

Example

Here, we are using the enumerate() function with both a list and a string. Creating enumerate objects for each and displaying their return types. It also shows how to change the starting index for enumeration when applied to a string, resulting in index-element pairs for the list and string.

Python
l1 = ["eat", "sleep", "repeat"]s1 = "geek"# creating enumerate objectsobj1 = enumerate(l1)obj2 = enumerate(s1)print ("Return type:", type(obj1))print (list(enumerate(l1)))# changing start index to 2 from 0print (list(enumerate(s1, 2)))

Output:

Return type: <class 'enumerate'>[(0, 'eat'), (1, 'sleep'), (2, 'repeat')][(2, 'g'), (3, 'e'), (4, 'e'), (5, 'k')]

Using Enumerate Object in Loops

Enumerate() is used with a list called l1. It first prints tuples of index and element pairs. Then it changes the starting index while printing them together. Finally, it prints the index and element separately, each on its own line.

Python
l1 = ["eat", "sleep", "repeat"]# printing the tuples in object directlyfor ele in enumerate(l1): print (ele)# changing index and printing separatelyfor count, ele in enumerate(l1, 100): print (count, ele)# getting desired output from tuplefor count, ele in enumerate(l1): print(count) print(ele)

Output:

(0, 'eat')(1, 'sleep')(2, 'repeat')100 eat101 sleep102 repeat0eat1sleep2repeat

Accessing the Next Element

In Python, the enumerate() function serves as an iterator, inheriting all associated iterator functions and methods. Therefore, we can use the next() function and __next__() method with an enumerate object.

To access the next element in an enumerate object, you can use the next() function. It takes the enumerate object as input and returns the next value in the iteration.

Python
fruits = ['apple', 'banana', 'cherry']enum_fruits = enumerate(fruits)next_element = next(enum_fruits)print(f"Next Element: {next_element}")

Output:

Next Element: (0, 'apple')

You can call next() again to retrieve subsequent elements:

Python
fruits = ['apple', 'banana', 'cherry']enum_fruits = enumerate(fruits)next_element = next(enum_fruits)print(f"Next Element: {next_element}")

Output:

Next Element: (0, 'apple')

Each time the next() is called, the internal pointer of the enumerate object moves to the next element, returning the corresponding tuple of index and value.

Enumerate() in Python – FAQs

How to use enumerate in Python?

Enumerate in Python is used to loop over an iterable and automatically provide an index for each item. It returns tuples with the index and corresponding value from the iterable. Example:

my_list = ['apple', 'banana', 'cherry']for index, value in enumerate(my_list): print(index, value)Output:0 apple1 banana2 cherry

This allows you to easily access both the index and the value of each item in the iterable during iteration.

How to get the index using enumerate in Python?

To get the index using enumerate in Python, you can use it in a for loop to iterate over an iterable (such as a list, tuple, or string). Here’s a simple example:

my_list = ['apple', 'banana', 'cherry']for index, value in enumerate(my_list): print(index, value)Output:0 apple1 banana2 cherry

In this example, enumerate(my_list) returns an enumerate object that generates tuples containing the index and value of each item in my_list during iteration. The for loop unpacks these tuples into index and value variables, allowing you to access both the index and the corresponding value of each element in the list.

How to enumerate in reverse in Python?

To enumerate in reverse in Python, you can combine enumerate with reversed. Here’s an example:

my_list = ['apple', 'banana', 'cherry']for index, value in enumerate(reversed(my_list)): print(len(my_list) - index - 1, value)Output:2 cherry1 banana0 apple

In this example, reversed(my_list) reverses the order of elements in my_list, and len(my_list) – index – 1 calculates the index in reverse order.

How to start enumerate at 1 in Python?

You can start enumerate at 1 in Python by specifying the start parameter. Here’s an example:

my_list = ['apple', 'banana', 'cherry']for index, value in enumerate(my_list, start=1): print(index, value)Output:1 apple2 banana3 cherry

In this example, enumerate(my_list, start=1) starts the index at 1 instead of the default 0.

What enumerate returns in Python?

In Python, enumerate() returns an enumerate object, which produces pairs of index and value from an iterable. It adds a counter to the iterable, making it easy to get both the index and the item during iteration. For example:

fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(index, fruit)
Output:
0 apple
1 banana
2 cherry


H

Harsh*t Agrawal

Improve

Previous Article

divmod() in Python and its application

Next Article

eval in Python

Please Login to comment...

Enumerate() in Python - GeeksforGeeks (2024)

References

Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6234

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.