Monday 14 August 2023

Iterate Over a Sequence with Indexes using enumerate method

Python's 'enumerate' function is used to iterate over sequences while keeping track of indexes. Here index can be a list, tuple, or string.

Example 1: Iterate over a list.

primes = [2, 3, 5, 7, 11]
for index, value in enumerate(primes):
    print(f'index : {index}, value : {value}')

 

Example 2: Iterate over a tuple.

employee = (1, 'Krishna', 34, 'Bangalore')
for index, value in enumerate(employee):
    print(f'index : {index}, value : {value}')

 

Example 3: Iterate over a string.

str = 'aeiou'
for index, value in enumerate(str):
    print(f'index : {index}, value : {value}')

 

You can specify a starting index other than the default 0 by specifying ‘start’ parameter.

str = 'aeiou'
for index, value in enumerate(str, start=2):
    print(f'index : {index}, value : {value}')

 

Above snippet prints below output.

index : 2, value : a
index : 3, value : e
index : 4, value : i
index : 5, value : o
index : 6, value : u

 

Find the below working application.

enumerate.py

# Iterate over a list
print('\nIterate over a list')
primes = [2, 3, 5, 7, 11]
for index, value in enumerate(primes):
    print(f'index : {index}, value : {value}')

# Iterate over a tuple
print('\nIterate over a tuple')
employee = (1, 'Krishna', 34, 'Bangalore')
for index, value in enumerate(employee):
    print(f'index : {index}, value : {value}')

# Iterate over a string
print('\nIterate over a string')
str = 'aeiou'
for index, value in enumerate(str):
    print(f'index : {index}, value : {value}')

# Iterate over a string from starting and index position at 2
print('\nIterate over a string from starting and index position at 2')
str = 'aeiou'
for index, value in enumerate(str, start=2):
    print(f'index : {index}, value : {value}')

 

Output

Iterate over a list
index : 0, value : 2
index : 1, value : 3
index : 2, value : 5
index : 3, value : 7
index : 4, value : 11

Iterate over a tuple
index : 0, value : 1
index : 1, value : Krishna
index : 2, value : 34
index : 3, value : Bangalore

Iterate over a string
index : 0, value : a
index : 1, value : e
index : 2, value : i
index : 3, value : o
index : 4, value : u

Iterate over a string from starting and index position at 2
index : 2, value : a
index : 3, value : e
index : 4, value : i
index : 5, value : o
index : 6, value : u

 

Can I iterate over an enumerate object multiple times?

 ‘enumerate’ object is an iterator internally, so once you've iterated through it, it will be exhausted and not available to iterate over it again.

 


enumerate_multiple_times.py
primes = [2, 3, 5, 7, 11]
enumerated_obj = enumerate(primes)

print('\nIterating over enumerated_obj')
for index, value in enumerated_obj:
    print(f'index : {index}, value : {value}')

print('\nIterating over enumerated_obj again')
for index, value in enumerated_obj:
    print(f'index : {index}, value : {value}')

 

Output

Iterating over enumerated_obj
index : 0, value : 2
index : 1, value : 3
index : 2, value : 5
index : 3, value : 7
index : 4, value : 11

Iterating over enumerated_obj again

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment