---Advertisement---

Python Enumerate() Function: Syntax, Examples, and Usage Explained Great 2025

By Bhavani

Updated On:

---Advertisement---
Python Enumerate() Function

๐Ÿ‘‰Python Enumerate() Function: Complete Guide with Examples

Python Enumerate() Function: The enumerate() function in Python is a powerful built-in feature that simplifies looping by adding a counter to iterable objects. It efficiently tracks indexes while iterating over lists, tuples, dictionaries, and strings.


๐Ÿ‘‰What is Python Enumerate()?

Python Enumerate() Function : The enumerate() function takes an iterable object and returns an enumerate object with a counter assigned to each item. This makes it easier to track index positions during iteration.


๐Ÿ‘‰Python Enumerate() Function Syntax

python

enumerate(iterable, startIndex)

๐Ÿ‘‰Parameters:

  • iterable: The object you want to loop through (e.g., list, tuple, string, etc.).
  • startIndex (optional): The starting count value. By default, the counter starts from 0.

๐Ÿ‘‰Return Value:


Returns an enumerate object containing pairs of index and corresponding values.


๐Ÿ‘‰Examples of Python Enumerate()

๐Ÿ‘‰1. Using enumerate() with a List

python

mylist = [‘A’, ‘B’, ‘C’, ‘D’]

e_list = enumerate(mylist)

print(list(e_list))

๐Ÿ‘‰Output:

css

[(0, ‘A’), (1, ‘B’), (2, ‘C’), (3, ‘D’)]


๐Ÿ‘‰2. Using enumerate() with a Start Index

python

mylist = [‘A’, ‘B’, ‘C’, ‘D’]

e_list = enumerate(mylist, 2)

print(list(e_list))

๐Ÿ‘‰Output:

[(2, ‘A’), (3, ‘B’), (4, ‘C’), (5, ‘D’)]

css


๐Ÿ‘‰3. Looping Over an Enumerate Object

python

mylist = [‘A’, ‘B’, ‘C’, ‘D’]

for i in enumerate(mylist):

    print(i)

print(“Using startIndex as 10”)

for i in enumerate(mylist, 10):

    print(i)

๐Ÿ‘‰Output:

vbnet

(0, ‘A’)

(1, ‘B’)

(2, ‘C’)

(3, ‘D’)

Using startIndex as 10

(10, ‘A’)

(11, ‘B’)

(12, ‘C’)

(13, ‘D’)


๐Ÿ‘‰4. Enumerating a Tuple

python

my_tuple = (“A”, “B”, “C”, “D”, “E”)

for i in enumerate(my_tuple):

    print(i)

๐Ÿ‘‰Output:

arduino

(0, ‘A’)

(1, ‘B’)

(2, ‘C’)

(3, ‘D’)

(4, ‘E’)


๐Ÿ‘‰5. Enumerating a String

python

my_str = “Python”

for i in enumerate(my_str):

    print(i)

๐Ÿ‘‰Output:

arduino

(0, ‘P’)

(1, ‘y’)

(2, ‘t’)

(3, ‘h’)

(4, ‘o’)

(5, ‘n’)


๐Ÿ‘‰6. Enumerating a Dictionary

python

my_dict = {“a”: “PHP”, “b”: “JAVA”, “c”: “PYTHON”, “d”: “NODEJS”}

for i in enumerate(my_dict):

    print(i)

๐Ÿ‘‰Output:

arduino

(0, ‘a’)

(1, ‘b’)

(2, ‘c’)

(3, ‘d’)


๐Ÿ‘‰Advantages of Using Enumerate in Python

โœ… Simplifies tracking index values in loops.
โœ… Efficient alternative to using list.index() in loops.
โœ… Reduces the need for manual counter management in iteration.
โœ… Supports multiple data structures like lists, tuples, dictionaries, and strings.


๐Ÿ‘‰Summary

The Python enumerate() function is an essential tool for developers working with iterable objects. By combining both index tracking and iteration, it offers a cleaner and more efficient way to manage data structures. Mastering enumerate() can significantly improve your coding efficiency in Python.

 Python Counter in Collections with Examples

Download Python

Leave a Comment

Index