Python3 enumerate() Function
enumerate() is a built-in Python function that gets both the index and the value while traversing an iterable.
Use enumerate() It conveniently provides both the index (position) of an element and the element itself, avoiding an extra counter variable.
Word meaning: enumerate means "enumerate", indicating listing one by one.
Basic syntax and parameters
Syntax format
enumerate(iterable, start=0)
Parameter Description
- Parameter iterable:
- Type: iterable
- Description: the iterable to enumerate.
- Parameter start(optional):
- Type: integer
- Description: the starting index value, defaults to 0.
Function description
- Return Value: returns an enumerate object (an iterator).
- Output: each iteration returns an (index, value) tuple.
Example
Example 1: basic usage
Example
Basic enumerate usage
fruits
=
[
"Apple"
,
"Banana"
,
"Orange"
]
Get the index and value
for
index
,
fruit
in
enumerate
(
fruits
)
:
(
f
"{index}: {fruit}"
)
Starts at 0 by default
(
"---"
)
for
index
,
fruit
in
enumerate
(
fruits
)
:
(
fruit
)
Expected output:
0: 苹果
1: 香蕉
2: 橙子
---
0: 苹果
1: 香蕉
2: 橙子
Code analysis:
- enumerate returns (index, value) tuples.
- Indexes start at 0 by default.
Example 2: specifying a starting index
Example
Number starting from 1
fruits
=
[
"Apple"
,
"Banana"
,
"Orange"
]
for
i
,
fruit
in
enumerate
(
fruits
,
start
=
1
)
:
(
f
"Fruit #{i}: {fruit}"
)
Start from 10
(
"---"
)
for
i
,
fruit
in
enumerate
(
fruits
,
start
=
10
)
:
(
f
"Index {i}: {fruit}"
)
Expected output:
第1个水果: 苹果
第2个水果: 香蕉
第3个水果: 橙子
---
索引 10: 苹果
索引 11: 香蕉
索引 12: 橙子
The start parameter can specify the starting index.
Example 3: converting to a list
Example
Convert to a list
fruits
=
[
"Apple"
,
"Banana"
,
"Orange"
]
result
=
list
(
enumerate
(
fruits
)
)
(
result
)
Output: [(0, 'Apple'), (1, 'Banana'), (2, 'Orange')]
Convert to a dictionary (if the values are strings)
result
=
dict
(
enumerate
(
fruits
)
)
(
result
)
Output: {0: 'Apple', 1: 'Banana', 2: 'Orange'}
With a list comprehension
squares
=
[
x**
2
for
x
in
range
(
5
)
]
indexed_squares
=
[
(
i
,
v
)
for
i
,
v
in
enumerate
(
squares
)
]
(
indexed_squares
)
Output: [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
Expected output:
enumerate can be converted conveniently to a list or a dictionary.
Example 4: practical applications
Example
Find the index of an element
fruits
=
[
"Apple"
,
"Banana"
,
"Orange"
,
"Banana"
,
"Apple"
]
target
=
"Banana"
for
index
,
fruit
in
enumerate
(
fruits
)
:
if
fruit
==
target:
(
f
"Found {target} at index {index}"
)
break
Find all matches with a condition
(
"\nAll positions of bananas:"
)
for
index
,
fruit
in
enumerate
(
fruits
)
:
if
fruit
==
"Banana"
:
(
index
,
end
=
" "
)
Output: 1 3
Find character positions in a string
text
=
"hello"
for
i
,
c
in
enumerate
(
text
)
:
(
f
"'{c}' is at position {i}"
)
Expected output:
enumerate is often used in scenarios needing an index, such as searching and replacing.
Example 5: used with dictionaries
Example
enumerate of a dictionary
person
=
{
"name"
:
"Tom"
,
"age"
:
20
,
"city"
:
"Beijing"
}
Traverse a dictionary's key/value pairs (requires items)
for
index
,
(
key
,
value
)
in
enumerate
(
person.
items
(
)
)
:
(
f
"{index}: {key} = {value}"
)
Create a dictionary with sequence numbers
items
=
[
"a"
,
"b"
,
"c"
]
numbered
=
{
i+
1
: v
for
i
,
v
in
enumerate
(
items
)
}
(
numbered
)
Output: {1: 'a', 2: 'b', 3: 'c'}
Expected output:
When used with a dictionary, enumerate requires the items() method.
Python3 built-in function