DEV Community

qing
qing

Posted on

TIL: enumerate() Has a start Parameter You Probably Never Used (2026)

TIL: enumerate() Has a start Parameter You Probably Never Used

When working with loops in Python, enumerate() is a powerful tool that allows us to iterate over a list while also keeping track of the index. However, most of us are used to the default behavior of enumerate(), where the index starts at 0. But did you know that enumerate() has a start parameter that lets you change the starting index?

By default, enumerate() works like this:

fruits = ['apple', 'banana', 'cherry']
for i, fruit in enumerate(fruits):
    print(f"{i}: {fruit}")
# Output:
# 0: apple
# 1: banana
# 2: cherry
Enter fullscreen mode Exit fullscreen mode

But with the start parameter, you can change the starting index to, say, 1:

fruits = ['apple', 'banana', 'cherry']
for i, fruit in enumerate(fruits, start=1):
    print(f"{i}: {fruit}")
# Output:
# 1: apple
# 2: banana
# 3: cherry
Enter fullscreen mode Exit fullscreen mode

As you can see, using enumerate(fruits, start=1) gives us a more human-friendly output, where the index starts at 1 instead of 0.

Takeaway: The next time you're working with enumerate(), remember that you can use the start parameter to customize the starting index, making your code more readable and user-friendly.


Follow me on Dev.to for daily Python tips and quick guides!


💡 Related: **Content Creator Ultimate Bundle (Save 33%)* — $30*


If you found this useful, you might like Python Interview Prep Guide — a practical resource that takes things a step further. At $24.99 it's a solid investment for your toolkit.


喜欢这篇文章?关注获取更多Python自动化内容!

Top comments (0)