Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 enumerate() Function

Python3 enumerate() Function

Python3 Built-in Functions Python3 built-in 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

- Type: iterable - Description: the iterable to enumerate. - Type: integer - Description: the starting index value, defaults to 0.

Function description


Example

Example 1: basic usage

Example

Basic enumerate usage

fruits

=

[

"Apple"

,

"Banana"

,

"Orange"

]

Get the index and value

for

index

,

fruit

in

enumerate

(

fruits

)

:

   

print

(

f

"{index}: {fruit}"

)

Starts at 0 by default

print

(

"---"

)

for

index

,

fruit

in

enumerate

(

fruits

)

:

   

print

(

fruit

)

Expected output:

0: 苹果
1: 香蕉
2: 橙子
---
0: 苹果
1: 香蕉
2: 橙子

Code analysis:

  1. enumerate returns (index, value) tuples.
  2. 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

)

:

   

print

(

f

"Fruit #{i}: {fruit}"

)

Start from 10

print

(

"---"

)

for

i

,

fruit

in

enumerate

(

fruits

,

start

=

10

)

:

   

print

(

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

)

)

print

(

result

)

 

Output: [(0, 'Apple'), (1, 'Banana'), (2, 'Orange')]

Convert to a dictionary (if the values are strings)

result

=

dict

(

enumerate

(

fruits

)

)

print

(

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

)

]

print

(

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:

       

print

(

f

"Found {target} at index {index}"

)

       

break

Find all matches with a condition

print

(

"\nAll positions of bananas:"

)

for

index

,

fruit

in

enumerate

(

fruits

)

:

   

if

fruit

==

"Banana"

:

       

print

(

index

,

end

=

" "

)

 

Output: 1 3

Find character positions in a string

text

=

"hello"

for

i

,

c

in

enumerate

(

text

)

:

   

print

(

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

(

)

)

:

   

print

(

f

"{index}: {key} = {value}"

)

Create a dictionary with sequence numbers

items

=

[

"a"

,

"b"

,

"c"

]

numbered

=

{

i+

1

: v

for

i

,

v

in

enumerate

(

items

)

}

print

(

numbered

)

 

Output: {1: 'a', 2: 'b', 3: 'c'}

Expected output:

When used with a dictionary, enumerate requires the items() method.


Python3 Built-in Functions Python3 built-in function