1

I know that

while True:
    condition to break loop

will result in an infinite loop. But I want to do the same with a 'for' statement. Somebody please help me

1
  • What exactly do you mean with "do the same with a for loop"? Commented Mar 21, 2016 at 15:20

3 Answers 3

7

Use infinite iterator like itertools.count, itertools.cycle, itertools.repeat which yield items infinitely:

for i in itertools.repeat(1):
    # do something
Sign up to request clarification or add additional context in comments.

Comments

0

You could use a for loop with a self made range, eg:

def endless_range(start, end, step):
    while start <= end:
        yield start
        start += step

for x in endless_range(0, 1, 0):
    condition to break loop

But why not using the while loop?

Comments

0

I can't tell exactly how you want your items to repeat, you may just want the following.

while True:
    for value in items:
        # ...
        if condition:
            break
        # ...
    if condition:
        break

1 Comment

This is not answering the question.

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.