The enumerate function in Python
Discover the secrets of the enumerate function in Python, which allows looping over an iterable while obtaining its position!
In Python, the native function enumerate()
allows iterating over an iterable while keeping count of the iterations. Enumerate() returns an object of type enumerate
containing the index and the iterated element. This object can be transformed into a list or tuple for further use.
Syntax of the enumerate function:
enumerate(iterable, start=0)
Parameters:
- iterable: an object that supports iteration
- start: the number from which to start the enumeration, by default
0
Example:
letters = ['a', 'b', 'c']
for position, letter in enumerate(letters):
print(position, letter)
0 a
1 b
2 c
💡 Want to learn more about native functions in Python like
enumerate()
? I have written an article covering all native functions in Python.
Why use enumerate?
One can easily loop over an iterable such as a list, tuple, or string with a for ... in ...
loop in Python. The only issue with using this syntax is that we do not have access to the index of the element. This is where the enumerate()
function becomes useful.
letters = ['a', 'b', 'c']
for letter in letters:
# We do not have access to the position of the letter in letters
print(letter)
Certainly, we could create an index that we increment by 1 at each loop iteration… but this is what enumerate()
allows to do without having to add extra lines of code.
How to use enumerate on a list?
The enumerate()
function works on objects that can be iterated over in Python. Therefore, the function can be used on lists in the following manner:
my_list = ['how', 'to', 'code']
for position, word in enumerate(my_list):
print(f'{position} : {word}')
This yields:
0 : how
1 : to
2 : code
We can also create an object containing the elements and their positions!
my_list = ['how', 'to', 'code']
print(tuple(enumerate(my_list)))
This returns:
((0, 'how'), (1, 'to'), (2, 'code'))
How to use enumerate on a tuple?
The enumerate()
function works on objects that can be iterated over in Python. Therefore, the function can be used on tuples in the following manner:
my_tuple = ('how', 'to', 'code')
for position, word in enumerate(my_tuple):
print(f'{position} : {word}')
This yields:
0 : how
1 : to
2 : code
We can also create an object containing the elements and their positions!
my_tuple = ('how', 'to', 'code')
print(tuple(enumerate(my_tuple)))
This returns:
((0, 'how'), (1, 'to'), (2, 'code'))
How to use enumerate on a string?
The enumerate()
function works on objects that can be iterated over in Python. Therefore, the function can be used on strings in the following manner:
my_string = 'abc'
for position, letter in enumerate(my_string):
print(f'{position} : {letter}')
This yields:
0 : a
1 : b
2 : c
We can also create an object containing the elements and their positions!
my_string = 'thomascollart.com'
print(tuple(enumerate(my_string)))
This returns:
((0, 'c'), (1, 'o'), (2, 'm'), (3, 'm'), (4, 'e'), (5, 'n'), (6, 't'), (7, 'c'), (8, 'o'), (9, 'd'), (10, 'e'), (11, 'r'), (12, '.'), (13, 'c'), (14, 'o'), (15, 'm'))
How to use enumerate from a specific position?
As you may remember, we saw in the introduction of this article that we can pass a second parameter to the native function enumerate()
in Python.
This second parameter allows us to define the value of the first index.
We are used to starting counting from 0 rather than 1. But if we want to start counting from 1
instead of 0
, it is enough to put this value as the second parameter of enumerate()
.
In practice, if we want to start counting from 1
instead of 0
, it looks like this:
my_string = 'abc'
print(tuple(enumerate(my_string, 1)))
This yields:
1 : a
2 : b
3 : c
Going further in Python with enumerate
Congratulations, you now know one more tool for clean coding in Python!
Feel free to check out other Python guides:
Hey, I'm Thomas 👋 Here, you'll find articles about tech, and what I think about the world. I've been coding for 20+ years, built many projects including Startups. Check my About/Start here page to know more :).