Post about getting index and member of iterable in iteration one at a time
Environment and Prerequisite
- Python
enumerate
enumerate built-in function
enumerate(iterable, start=0)
- It is a built-in function which returns an enumerate object.
Iterable
object is function parameter. There is reference aboutiterable
in [Python](EN) Iterable and Iterator . enumerate(iterable, start=0)
returnsiterable
’s member and its index as tuple.- Its form is
(index, member)
when it returns.
enumerate example
>>> fruits = ["Apple", "Banana", "Grape", "Mango", "Orange"]
>>> list(enumerate(fruits))
[(0, 'Apple'), (1, 'Banana'), (2, 'Grape'), (3, 'Mango'), (4, 'Orange')]
>>> for pair in enumerate(fruits):
... print(pair)
...
(0, 'Apple')
(1, 'Banana')
(2, 'Grape')
(3, 'Mango')
(4, 'Orange')
>>> for idx, fruit_name in enumerate(fruits):
... print(f"{idx} and {fruit_name}")
...
0 and Apple
1 and Banana
2 and Grape
3 and Mango
4 and Orange