Introduction

One of the most useful built-in functions in Python is the enumerate function, which provides a way to iterate over a sequence while keeping track of the index of each item. This tutorial will explore the enumerate function in Python, how it works, and how it can be used to simplify your code and make it more efficient. You'll also learn about the different use cases for the function and how to incorporate it into your own coding projects.

Table of Contents :

  • Introduction to the Enumerate function
  • Syntax of the Enumerate function 
  • Enumerate function with different data types
    • Enumerate function with a list
    • Enumerate function with a string
    • Enumerate function with a tuple
    • Enumerate function with a dictionary
  • Specifying the start value for enumeration
  • Enumerate function with nested loops
  • Enumerate function with unpacking

Introduction to the Enumerate function :

  • The enumerate() function in Python is used to iterate over an iterable (such as a list, string, tuple, or dictionary)
  • It also provides an index along with the corresponding element.
  • It simplifies the process of iterating over elements while also keeping track of their indices.
  • The Enumerate function is useful when you need both the index and the value of an element during iteration.

Syntax of the Enumerate function :

  • The syntax of the Enumerate function is :  enumerate(iterable, start=0) 
    • The iterable parameter is the object or sequence that you want to iterate over.
    • The optional start parameter specifies the starting index for enumeration. It is 0 by default.

Enumerate function with different data types :

In this tutorial we'll have a look at the usage of enumerate function with different data types :

  • List
  • string
  • tuple
  • dictionary

Enumerate function with a list :

  • We can use the Enumerate function with a list to iterate over its elements while getting their indices.
  • Sample code :

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

Enumerate function with a string :

  • The Enumerate function can also be used with a string to iterate over its characters while obtaining their indices.
  • Sample code :

message = "Hello"
for index, char in enumerate(message):
    print(index, char)
    

# Output

# 0 H
# 1 e
# 2 l
# 3 l
# 4 o


Enumerate function with a tuple :

  • You can apply the Enumerate function to a tuple to iterate over its elements and access their indices.
  • Sample code :

animals = ('cat', 'dog', 'rabbit')
for index, animal in enumerate(animals):
    print(index, animal)
    
# Output

# 0 cat
# 1 dog
# 2 rabbit
    

Enumerate function with a dictionary  :

  • The Enumerate function can be used with a dictionary to iterate over its keys while obtaining their indices.
  • Sample code :

scores = {'Alice': 85, 'Bob': 92, 'Charlie': 78}
for index, key in enumerate(scores):
    print(index, key, scores[key])
    

# Output

# 0 Alice 85
# 1 Bob 92
# 2 Charlie 78    

Specifying the start value for enumeration :

  • By default, the enumeration starts from 0. 
  • However, we can specify a custom start value using the start parameter.
  • Sample code :

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

# 1 apple
# 2 banana
# 3 cherry   
 

Enumerate function with nested loops :

  • The Enumerate function can be used in nested loops to iterate over multidimensional data structures while keeping track of indices.
  • Sample code :

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for i, row in enumerate(matrix):
    for j, value in enumerate(row):
        print(i, j, value)
        
# Output

# 0 0 1
# 0 1 2
# 0 2 3
# 1 0 4
# 1 1 5
# 1 2 6
# 2 0 7
# 2 1 8
# 2 2 9       
 

Enumerate function with unpacking :

  • The Enumerate function can be combined with unpacking to access individual elements of an iterable.
  • Sample code :

coordinates = [(1, 2), (3, 4), (5, 6)]
for index, (x, y) in enumerate(coordinates):
    print(index, x, y)
    
# Output

# 0 1 2
# 1 3 4
# 2 5 6    


Prev. Tutorial : Pass statement

Next Tutorial : What are functions