Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid counter increment in Python loop

Tags:

python

How can I avoid having to increment the sequence counter manually in the following Python code:

sequence = 0
for value in random.sample(range(1000), 7):
  # do something with value and sequence
  sequence += 1
like image 552
necromancer Avatar asked Jul 24 '26 14:07

necromancer


1 Answers

Enumerate! You can refer to the Python docs.

for sequence, value in enumerate(random.sample(range(1000), 7)):
    # do something with value and sequence
like image 172
Prashant Kumar Avatar answered Jul 26 '26 05:07

Prashant Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!