Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Mastering Python’s enumerate(): Loop with Indices for Lists, Tuples, Strings, and Dictionaries

What Is Python’s enumerate?

enumerate() is a built‑in Python function that augments any iterable with a counter, returning an iterator of (index, value) tuples. According to the official Python documentation, the function provides a concise, efficient way to access both the position and the element during iteration.

Key Takeaways

Syntax

enumerate(iterable, start=0)

Parameters

Return Value

An iterator yielding pairs of the form (index, element), where index starts at start and increments by one for each element.

Using enumerate() – Basic Example

Below is a simple demonstration. The list my_list is passed to enumerate(), and the resulting iterator is converted to a list for display.

my_list = ['A', 'B', 'C', 'D']
print(list(enumerate(my_list)))

Output:

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

Custom Start Index

By supplying the start argument, you can shift the numbering:

my_list = ['A', 'B', 'C', 'D']
print(list(enumerate(my_list, 2)))

Output:

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

Looping with enumerate()

Using enumerate() inside a for loop keeps the code clean and eliminates the need for a separate counter variable.

my_list = ['A', 'B', 'C', 'D']
for index, value in enumerate(my_list):
    print(index, value)

print("---")

for index, value in enumerate(my_list, 10):
    print(index, value)

Output:

0 A
1 B
2 C
3 D
---
10 A
11 B
12 C
13 D

Enumerating a Tuple

The same pattern applies to tuples:

my_tuple = ('A', 'B', 'C', 'D', 'E')
for index, value in enumerate(my_tuple):
    print(index, value)

Output:

0 A
1 B
2 C
3 D
4 E

Enumerating a String

Strings are iterable in Python, so enumerate() can reveal the position of each character:

my_str = "Guru99"
for index, char in enumerate(my_str):
    print(index, char)

Output:

0 G
1 u
2 r
3 u
4 9
5 9

Enumerating a Dictionary

When you pass a dictionary to enumerate(), it iterates over the keys:

my_dict = {"a": "PHP", "b": "JAVA", "c": "PYTHON", "d": "NODEJS"}
for index, key in enumerate(my_dict):
    print(index, key)

Output:

0 a
1 b
2 c
3 d

Why Use enumerate()?

Summary

Python

  1. Mastering Python's While Loop: Syntax, Examples, and Best Practices
  2. Master Python Functions: Syntax, Types, and Practical Examples
  3. Mastering Python Function Arguments: Positional, Keyword, and Default Parameters
  4. Mastering Python Tuples: Creation, Access, and Advanced Operations
  5. Master Python Strings: Creation, Formatting, and Manipulation
  6. Python Closures Explained: How Nested Functions Capture Variables
  7. Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
  8. Python len(): A Practical Guide to Measuring Object Lengths
  9. Understanding Python's Main Function: A Practical Guide to def main()
  10. Mastering Python’s Yield: Generator vs Return – A Practical Guide