In this tutorial, we will learn about the Python enumerate[] method with the help of examples.
The enumerate[]
method adds a counter to an iterable and returns it [the enumerate object].
Example
languages = ['Python', 'Java', 'JavaScript']
enumerate_prime = enumerate[languages]
# convert enumerate object to list
print[list[enumerate_prime]]
# Output: [[0, 'Python'], [1, 'Java'], [2, 'JavaScript']]
Syntax of enumerate[]
The syntax of enumerate[]
is:
enumerate[iterable, start=0]
enumerate[] Parameters
enumerate[]
method takes two parameters:
- iterable - a sequence, an iterator, or objects that supports iteration
- start [optional] -
enumerate[]
starts counting from this number. If start is omitted,0
is taken asstart
.
enumerate[] Return Value
enumerate[]
method adds counter to an iterable and returns it. The returned object is an enumerate object.
You can convert enumerate objects to list and tuple using list[] and tuple[] method respectively.
Example 1: How enumerate[] works in Python?
grocery = ['bread', 'milk', 'butter']
enumerateGrocery = enumerate[grocery]
print[type[enumerateGrocery]]
# converting to list
print[list[enumerateGrocery]]
# changing the default counter
enumerateGrocery = enumerate[grocery, 10]
print[list[enumerateGrocery]]
Output
[[0, 'bread'], [1, 'milk'], [2, 'butter']] [[10, 'bread'], [11, 'milk'], [12, 'butter']]
Example 2: Looping Over an Enumerate object
grocery = ['bread', 'milk', 'butter']
for item in enumerate[grocery]:
print[item]
print['\n']
for count, item in enumerate[grocery]:
print[count, item]
print['\n']
# changing default start value
for count, item in enumerate[grocery, 100]:
print[count, item]
Output
[0, 'bread'] [1, 'milk'] [2, 'butter'] 0 bread 1 milk 2 butter 100 bread 101 milk 102 butter
In Python, you can get the element and index [count] from iterable objects such as list
and tuple
in for
loop with the built-in function enumerate[]
.
- Built-in Functions - enumerate[] — Python 3.8.5 documentation
This article describes the following contents.
- How to use
enumerate[]
- Normal
for
loop for
loop withenumerate[]
- Normal
- Start index at 1 with
enumerate[]
- Set step with
enumerate[]
See the following articles for more information about for
loop and how to use enumerate[]
and zip[]
together.
- for loop in Python [with range, enumerate, zip, etc.]
- Use enumerate[] and zip[] together in Python
How to use enumerate[]
Normal for
loop
l = ['Alice', 'Bob', 'Charlie']
for name in l:
print[name]
# Alice
# Bob
# Charlie
for
loop with enumerate[]
By passing an iterable object to enumerate[]
, you can get index, element
.
for i, name in enumerate[l]:
print[i, name]
# 0 Alice
# 1 Bob
# 2 Charlie
Start index at 1 with enumerate[]
As in the example above, by default, the index of enumerate[]
starts at 0.
If you want to start from another number, pass the number to the second
argument of enumerate[]
.
Start at 1:
for i, name in enumerate[l, 1]:
print[i, name]
# 1 Alice
# 2 Bob
# 3 Charlie
Start at the other number:
for i, name in enumerate[l, 42]:
print[i, name]
# 42 Alice
# 43 Bob
# 44 Charlie
For example, this is useful when generating sequential number strings starting from 1. It is smarter to pass the starting number to the second argument of enumerate[]
than to calculate i + 1
.
for i, name in enumerate[l, 1]:
print['{:03}_{}'.format[i, name]]
# 001_Alice
# 002_Bob
# 003_Charlie
Set step with enumerate[]
There is no argument like step
to specify increment to enumerate[]
, but it can be done as follows.
step = 3
for i, name in enumerate[l]:
print[i * step, name]
# 0 Alice
# 3 Bob
# 6 Charlie