Featured Article

Master the Enumerate Function Python For Loop A Complete Guide

Nicole Jul 13, 2026

The enumerate function in Python is a built-in function that allows us to loop over a sequence or other iterable object and have the iterator item and its index accessible simultaneously. It's a powerful tool for simple yet effective iteration, making your code more readable and efficient.

Python for Loops: The Pythonic Way – Real Python
Python for Loops: The Pythonic Way – Real Python

Let's dive into the world of enumeration in Python with the enumerate function. We'll explore its syntax, walkthrough examples, and its various use cases.

Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet

Understanding Enumerate Function

The syntax of enumerate function is quite straightforward. It takes an iterable as an argument and returns an enumerate object that produces a sequence of tuples, each containing a count (from start, which defaults to 0) and the values obtained from iterating over the iterable.

python Loops for loop and while loop
python Loops for loop and while loop

Here's a simple example illustrating how to use the enumerate function with a list:

Basic Enumerate Example

Python for loop
Python for loop

Let's consider a list of fruits ['apple', 'banana', 'cherry']:

```python fruits = ['apple', 'banana', 'cherry'] for i, fruit in enumerate(fruits): print(i, fruit) ```

The output will be:

``` 0 apple 1 banana 2 cherry ```

As you can see, enumerate adds an index to each element in the list while loop iteration takes place.

an image of a computer screen with some text on it
an image of a computer screen with some text on it

Starting Index

By default, enumerate starts indexing from 0. However, this can be changed by providing a second argument to the function. Let's see how to start from 1:

```python for i, fruit in enumerate(fruits, start=1): print(i, fruit) ```

The output will be:

Write Cleaner Python Loops with enumerate()
Write Cleaner Python Loops with enumerate()

``` 1 apple 2 banana 3 cherry ```

This can be particularly useful when you want your index to match the position of items in an iterable.

Enumerate Function with For Loop

Important Functions in Python
Important Functions in Python
a black background with text that reads what is enumerate in python?
a black background with text that reads what is enumerate in python?
Python For Loop | How To Use Python For Loops? | For Loops ⋆
Python For Loop | How To Use Python For Loops? | For Loops ⋆
Python Loops Explained for Beginners (Easy Guide)
Python Loops Explained for Beginners (Easy Guide)
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
18 Python while Loop Examples and Exercises
18 Python while Loop Examples and Exercises
Python Control Flow for Beginners – If Else, For Loop, While Loop Explained
Python Control Flow for Beginners – If Else, For Loop, While Loop Explained
Python Loops Complete Cheatsheet 🌀 | for, while, break, continue, nested loops & patterns
Python Loops Complete Cheatsheet 🌀 | for, while, break, continue, nested loops & patterns
Loops in Python 💻
Loops in Python 💻

The for loop is the most common way to utilize the enumerate function. It allows you to traverse list items along with their index in a sequential manner.

For Loop Syntax

The syntax for using enumerate with a for loop is as follows:

```python for index, element in enumerate(iterable): # loop body ```

Here, index and element are placeholders. You can replace them with any suitable identifiers according to your needs.

For Loop Example

Let's consider a list of tuples, where each tuple contains the name and price of a food item. We would like to add a discount and print the discounted price for each item. We can use the enumerate function along with the for loop to achieve this:

```python foods = [('apple', 5.00), ('banana', 2.50), ('cherry', 10.00)] for i, (name, price) in enumerate(foods): discount = price * 0.05 # calculate 5% discount discounted_price = price - discount print(f"{i + 1}. {name}: ${discounted_price:.2f}") ```

The output will be:

``` 1. apple: $4.75 2. banana: $2.38 3. cherry: $9.50 ```

In this example, we not only accessed the names and prices, but also manipulated the data and formatted the output.

Using enumerate with the for loop enhances your coding capability and improves the readability of your Python program.

Embrace the power of enumeration in Python. Now you're equipped with the knowledge and examples to harness the full potential of the enumerate function. Happy enumerating!