Tested on Python 3.12
Tested on Python 3.12
Here'sHere are twelve examples of how you can access the indices with their corresponding array's elements using for loops, while loops and some looping functions. Note that array indices always start from zero by default (see example 4
to change this).
items = [8, 23, 45, 12, 78]
for i, item in enumerate(items, start=1000start=100):
print(i, item)
Result:
# 1000100 8
# 1001101 23
# 1002102 45
# 1003103 12
# 1004104 78
Result:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
12. Also, it's nice to iterate list's elements inside class' Static Method
.
items = [8, 23, 45, 12, 78]
class ElementPlus:
@staticmethod # decorator
def indexForEachOfMy(iterable):
for pair in enumerate(iterable):
print pair
ElementPlus.indexForEachOfMy(items)
Result:
# (0, 8)
# (1, 23)
# (2, 45)
# (3, 12)
# (4, 78)