Built-in Functions
enumerate()
Pair each item with a counter.
enumerate(iterable, start=0)
enumerate() returns an enumerate object yielding (index, item) pairs for each element in the iterable.
The starting count can be adjusted with the optional start parameter (default 0).
Parameters
| Name | Description |
|---|---|
| iterable | The sequence or other iterable to count through. |
| start | The first counter value; it defaults to 0. |
Returns
An enumerate iterator that produces index-item pairs.
Try it
Run it and change it
names = ["Ada", "Lin"]
for index, name in enumerate(names, start=1):
print(index, name)
Worth knowing
- Custom start index: Pass
start=1for 1-based numbering:for i, item in enumerate(items, start=1):.