Question

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?

Was it helpful?

Solution

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++

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top