3

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

1 Answer 1

8

Enumerate! You can refer to the Python docs.

for sequence, value in enumerate(random.sample(range(1000), 7)):
    # do something with value and sequence
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.