1# Basic syntax:
2for iteration, item in enumerate(iteratable_object):
3 print(iteration)
4# Where:
5# - item is the current element in the iteratable_object
6# - iteration is the iteration number/count for the current iteration
7
8# Basic syntax using list comprehension:
9[iteration for iteration, item in enumerate(iteratable_object)]
10
11# Example usage:
12your_list = ["a", "b", "c", "d"]
13
14for iteration,item in enumerate(your_list):
15 (iteration, item) # Return tuples of iteration, item
16--> (0, 'a')
17 (1, 'b')
18 (2, 'c')
19 (3, 'd')
20
21[iteration for iteration, item in enumerate(your_list)]
22--> [0, 1, 2, 3]
1>>> a = ['foo', 'bar', 'baz']
2>>> for i in a:
3... print(i)
4...
5foo
6bar
7baz
8
1# Iterate over dictionary using enumerate()
2
3mydict = {
4 "one": 1,
5 "two": 2,
6 "three": 3
7}
8
9# You can replace `index` with an underscore to ignore the index
10# value if you don't plan to use it.
11for index, item in enumerate(mydict):
12 print(f"{item}: {mydict.get(item)}")
13
14# OUTPUT:
15# one: 1
16# two: 2
17# three: 3