Dineshbaburam
Mar 15, 2022

--

Enumerate in python

Enumerate add an iterable counter and return enumerate object. Enumerate objects can be directly used in for loop or convert into the list of tuples using the list() method.

some_string = "python"
some_dict = {}
print("Convert it into list")
print(list(enumerate(some_string)))
for i, some_dict[i] in enumerate(some_string):
pass
print("Dict")
print(some_dict)

The output

Convert it into list
[(0, 'p'), (1, 'y'), (2, 't'), (3, 'h'), (4, 'o'), (5, 'n')]
Dict
{0: 'p', 1: 'y', 2: 't', 3: 'h', 4: 'o', 5: 'n'}

--

--