0

first, I'd like to say I'm sorry because my english is not good enough but hopefully you'll understand me. I searched internet and couldn't find how to do this in python 2.7, I know it could be done with while loop but I'd like to know if it is possible to be done with for, also.

if we have a for loop that looks like this

for counter in range(0,len(list)):
    if (condition):
        var=something from dictionary

and i need to repeat the same iteration of loop if this condition is true. Tried with i=i-1 but that didn't work and I later found out that python creates a list of numbers on the beginning of a loop, tried xrange also but didn't work. So, does anyone know how to do this?

1
  • Put a while condition inside the for loop? Commented May 13, 2014 at 20:58

2 Answers 2

1

What you need here is a while loop. It will be much easier for you to control the iterations inside the loop. You can structure it like this:

i = 0
while i < len(list):
    if condition:
        var = something from dictionary
    else:
        i++
Sign up to request clarification or add additional context in comments.

2 Comments

Did it like this actually, but in c could use for and repeating iteretions so wanted to know if this could be done in python too
Read this from the python docs on the difference between the for loop in python and its C counterpart. docs.python.org/2/tutorial/controlflow.html
0

have you tried

for l in list:
    if(condition):
        var=getValue()

for more examples details you will probably find a good examples at Python ForLoop

1 Comment

need this for pushdown automata so this doesn't work

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.