Enumerate with different Start in Python
Python - Enumerate with different start
In enumerate() builtin function, the default value for start parameter is zero. We can override this behavior by setting a specific value for start parameter. This lets us enumerate a given iterable with a different starting index.
Examples
1. Enumerate with start=2
In this example, we take a list myList
, and create an enumerated object from the given list, with a start index of 2
.
Python Program
myList = ['apple', 'banana', 'mango', 'cherry']
enumeratedList = enumerate(myList, start=2)
for item in enumeratedList:
print(item)
Output
(2, 'apple')
(3, 'banana')
(4, 'mango')
(5, 'cherry')
2. Enumerate with start=-10 (Negative Index)
In this example, we take a list myList
, and create an enumerated object from the given list, with a start index of -10
, a negative value.
Python Program
myList = ['apple', 'banana', 'mango', 'cherry']
enumeratedList = enumerate(myList, start=-10)
for item in enumeratedList:
print(item)
Output
(-10, 'apple')
(-9, 'banana')
(-8, 'mango')
(-7, 'cherry')
Summary
In this tutorial of Python Examples, we learned how to enumerate an iterable with a specific starting index.