[Python](EN) Get index and member of iterable in iteration using enumerate

Post about getting index and member of iterable in iteration one at a time

Environment and Prerequisite

  • Python


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 about iterable in [Python](EN) Iterable and Iterator .
  • enumerate(iterable, start=0) returns iterable’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