Python 内建函数列表 > Python 的内置函数 enumerate Python 的内置函数 enumerate 是一个非常有用的工具函数,主要用于在遍历序列(如列表、元组或字符串)时,同时获取元素的索引和值。

基本语法如下:

enumerate(iterable, start=0)

其中:

  • iterable 表示任何可迭代对象
  • start 是可选参数,指定索引的起始值,默认为 0

使用示例:

fruits = ['apple', 'banana', 'orange']
for index, fruit in enumerate(fruits):
    print(index, fruit)

运行结果:

0 apple
1 banana
2 orange