This postion is a notebook to collect the pythonic code I fetch when do code research.
it's a common requirement and some guys achieve this goal by using Numpy lib, but it's too heavy. you can do in this way:
import random
for _ in range(10)
print(random.randint(0, 100))
# the _ is from 0 - 9a = ['a','b','c','d']
for idx, val in enumerate(a):
print(f'idx = {idx}, val = {val}')
# Output:
# idx = 0, val = a
# idx = 1, val = b
# idx = 2, val = c
# idx = 3, val = dif you want to specify the start index, you can add a second parameter to enumerate func
# in this case, idx would start from 3
a = ['a','b','c','d']
for idx, val in enumerate(a, 3):
print(f'idx = {idx}, val = {val}')
# Output:
# idx = 3, val = a
# idx = 4, val = b
# idx = 5, val = c
# idx = 6, val = d
Suggest:
a = ['a', 'b', 'c', 'd']
for idx, val in enumerate(a, 3):
print(f'idx = {idx} val = {val}' )
'''
output:
idx = 3 val = a
idx = 4 val = b
idx = 5 val = c
idx = 6 val = d
'''