×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python - Enumerate a List

Last Updated : May 01, 2025

In Python, the enumerate() function allows you to loop through a list while keeping track of both the index and the item. In this chapter, we will learn how to use enumerate() in loops with the help of examples.

Enumerate a List Using enumerate() Function in a For Loop

The enumerate() function adds a counter to an iterable and returns it as an enumerate object.

Example

In this example, we are enumerating through a list of city names and printing both the index and the city.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Using enumerate to get index and city
for index, city in enumerate(cities):
    print(index, city)

The output of the above code would be:

0 New York
1 London
2 Paris
3 Tokyo
4 Sydney

Enumerate a List Starting Index from a Custom Number

The enumerate() function allows you to specify a starting index using the start parameter.

Example

In this example, we are starting the index from 101 while enumerating the list of cities.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Starting enumeration from 101
for index, city in enumerate(cities, start=101):
    print(index, city)

The output of the above code would be:

101 New York
102 London
103 Paris
104 Tokyo
105 Sydney

Converting the Enumerate Object to a List

You can convert the result of enumerate() into a list of tuples using the list() function.

Example

In this example, we are converting the enumerated result of city names into a list of index-city tuples.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Converting enumerate object to a list of tuples
indexed_cities = list(enumerate(cities))

# Displaying the result
print(indexed_cities)

The output of the above code would be:

[(0, 'New York'), (1, 'London'), (2, 'Paris'), (3, 'Tokyo'), (4, 'Sydney')]

Using Enumerate in List Comprehension

enumerate() can be used inside a list comprehension to create a new list with both indexes and items.

Example

In this example, we are creating a list of formatted strings using enumerate inside a list comprehension.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Using list comprehension with enumerate
formatted = [f"{i}: {city}" for i, city in enumerate(cities)]

# Displaying the formatted list
print(formatted)

The output of the above code would be:

['0: New York', '1: London', '2: Paris', '3: Tokyo', '4: Sydney']

Enumerate with Conditional Logic

Using enumerate() helps when you want to apply conditions based on the index while iterating through a list.

Example

In this example, we are printing cities only at even indexes using a conditional statement with enumerate.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Printing cities at even indexes
for i, city in enumerate(cities):
    if i % 2 == 0:
        print(f"Index {i} has city: {city}")

The output of the above code would be:

Index 0 has city: New York
Index 2 has city: Paris
Index 4 has city: Sydney

Exercise

Select the correct option to complete each statement about using enumerate() with lists in Python.

  1. What does the enumerate() function in Python return?
  2. Which of the following is the correct way to use enumerate() in a for loop?
  3. What will list(enumerate(['a', 'b', 'c'])) return?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.