Python's for loops are different. i gets reassigned to the next value every time through the loop.

The following will do what you want, because it is taking the literal version of what C++ is doing:

i = 0
while i < some_value:
    if cond...:
        i+=1
    ...code...
    i+=1

Here's why:

in C++, the following code segments are equivalent:

for(..a..; ..b..; ..c..) {
    ...code...
}

and

..a..
while(..b..) {
     ..code..
     ..c..
}

whereas the python for loop looks something like:

for x in ..a..:
    ..code..

turns into

my_iter = iter(..a..)
while (my_iter is not empty):
    x = my_iter.next()
    ..code..
Answer from jakebman on Stack Overflow
๐ŸŒ
Python Course
python-course.eu โ€บ python-tutorial โ€บ for-loops.php
20. For Loops | Python Tutorial | python-course.eu
Generally it has the form: for ... of this kind of loop is the for-loop of the programming language C: for (i=0; i <= n; i++) This kind of for loop is not implemented in Python!...
Discussions

A single python loop from zero to n to zero - Stack Overflow
A new AI Addendum clarifies how Stack Overflow utilizes AI interactions. Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... Is there a Pythonesque way to create a loop that traverses a range from 0 to n and then back to 0? I could just create 2 loops (one forward ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do you start a for loop at 1 instead of 0?
Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission: You are looping over an object using something like for x in range(len(items)): foo(item[x]) This is simpler and less error prone written as for item in items: foo(item) If you DO need the indexes of the items, use the enumerate function like for idx, item in enumerate(items): foo(idx, item) More on reddit.com
๐ŸŒ r/learnpython
16
6
November 13, 2015
Need to set i as zero everytime the for loop is run in python - Stack Overflow
In the second for loop that runs till 26 I want it to be set back to zero every time an item is removed from the list, so that I can check from the beginning if an alphabet is present in the set of... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I implement " for (int i=0; i<str.size; i= i*5) " in python using for loop?
Can I use range() here, or is there any other way? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Quora
quora.com โ€บ How-would-I-put-for-int-I-10-I-0-i-in-Python
How would I put for (int I = 10; I >= 0; i---) in Python? - Quora
In Python, you can achieve the same result using a for loop with range(). Here's how I would do it: ... This loop starts at 10, goes down to 0 (inclusive), and decrements by 1 on each iteration.
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ ForLoop
ForLoop - Python Wiki
The Python for statement iterates over the members of a sequence in order, executing the block each time. Contrast the for statement with the ''while'' loop, used when a condition needs to be checked each iteration or to repeat a block of code forever. For example: For loop from 0 to 2, therefore running 3 times.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-do-this-in-Python-for-I-0-j-n-I-n-j-0-I++-j
How to do this in Python: for (I=0, j=n; I =0; I++, j--) - Quora
2) If you want exactly n iterations with j starting at n and decreasing (so i runs 0..n-1 and j runs n..1): ... Use Python's range with paired iteration; the idiomatic options below produce the same effect as the C loop for (i = 0, j = n; i < n and j >= 0; i++, j--):
Find elsewhere
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ for-loops-in-python
For Loops in Python Tutorial: How to iterate over Pandas DataFrame | DataCamp
July 19, 2019 - Like other programming languages, for loops in Python are a little different in the sense that they work more like an iterator and less like a for keyword. In Python, there is not C like syntax for(i=0; i<n; i++) but you use for in n.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_for_loops.asp
Python For Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
Top answer
1 of 1
3

You can just use while for this:

i = 0 // maybe 1 would be better?
while i < someSize:
    doSomething()
    i *= 5

Just be aware that this is an infinite loop (unless someSize is zero, of course) if, as you did in your title, you start at zero. It doesn't matter how many time you multiply zero by five, you'll always end up with zero.


As an aside, there's nothing to stop you creating your own iterator, even one that calls a function to decide the next value and whether the iterator should stop.

The following code provides such a beast, one where you specify the initial value and a pair of functions to update and check the values (implemented as lambdas in the test code):

# Iterator which uses a start value, function for calculating next value,
# and function for determining whether to continue, similar to standard
# C/C++ for loop (not C++ range-for, Python already does that).

class FuncIter(object):
    def __init__(self, startVal, continueProc, changeProc):
        # Store all relevant stuff.

        self.currVal = startVal
        self.continueProc = continueProc
        self.changeProc = changeProc

    def __iter__(self):
        return self

    def __next__(self):
        # If allowed to continue, return current and prepare next.

        if self.continueProc(self.currVal):
            retVal = self.currVal
            self.currVal = self.changeProc(self.currVal)
            return retVal

        # We're done, stop iterator.

        raise StopIteration()

print([item for item in FuncIter(1, lambda x: x <= 200, lambda x: x * 4 - 1)])

The expression:

FuncIter(1, lambda x: x <= 200, lambda x: x * 4 - 1)

generates the values that you would get with the equivalent C/C++ for statement:

for (i = 1; i <= 200; i = i * 4 - 1)

as shown by the output:

[1, 3, 11, 43, 171]
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 72195945 โ€บ what-type-of-loops-are-i-for-i-in-x-for-i-0-in-python
what type of loops are [i for i in x for i! =0] in python? - Stack Overflow
say I have a list: c = ['A','B','C',''] I loop over it to print without the empty element '' using this code: for i in c: if i != '': print(i) i = i else: pass
๐ŸŒ
Snakify
snakify.org โ€บ for loop with range
For loop with range - Learn Python 3 - Snakify
result = 0 n = 5 for i in range(1, n + 1): result += i # this ^^ is the shorthand for # result = result + i print(result) Pay attention that maximum value in range() is n + 1 to make i equal to n on the last step. To iterate over a decreasing sequence, we can use an extended form of range() with three arguments - range(start_value, end_value, step). When omitted, the step is implicitly equal to 1. However, can be any non-zero value. The loop always includes start_value and excludes end_value during iteration:
๐ŸŒ
Learn Python
learnpython.org โ€บ en โ€บ Loops
Loops - Learn Python - Free Interactive Python Tutorial
If a break statement is executed inside the for loop then the "else" part is skipped. Note that the "else" part is executed even if there is a continue statement. ... # Prints out 0,1,2,3,4 and then it prints "count value reached 5" count=0 while(count<5): print(count) count +=1 else: print("count value reached %d" %(count)) # Prints out 1,2,3,4 for i in range(1, 10): if(i%5==0): break print(i) else: print("this is not printed because for loop is terminated because of break but not due to fail in condition")
๐ŸŒ
Real Python
realpython.com โ€บ python-for-loop
Python for Loops: The Pythonic Way โ€“ Real Python
February 23, 2026 - Pythonโ€™s for loop iterates over items in a data collection, allowing you to execute code for each item. To iterate from 0 to 10, you use the for index in range(11): construct.
๐ŸŒ
Quora
quora.com โ€บ Why-do-you-use-I-0-in-while-loops
Why do you use I=0 in while loops? - Quora
It makes no difference in a for loop. The difference between ++i and i++ is when you are trying to use the value of i in the same expression you are incrementing it. An example is: ... Here, in b = ++a, a is first incremented to 1 and then put into b, which makes b equal to 1. Then, in c = d++, d is first assigned to c and then incremented, leaving c as 0!
Top answer
1 of 2
1

You could use numpy to iterate through an array and print the sequence:

import numpy
for e in numpy.array([[[1,2],[1,2]],[[1,2],[1,2]]]).flatten(): print(e)

Just replace that list with your list.

2 of 2
0

The below explanation will be a little bit generic but hopefully after reading it you'll got the basic knowledge to solve your particular or more complex problem that deal with high-dimensional arrays.

ALLOCATION (GENERAL CASE)

Let's start by reviewing a simple way of allocating space for arrays in python:

1d array: lst = [0]*K0
2d array: lst = [0]*K1*K0
3d array: lst = [0]*K2*K1*K0
4d array: lst = [0]*K3*K2*K1*K0
...
Nd array: lst = [0]*Kn*Kn-1*...*K0

The most typical dimensional arrays would be (1D, 2D, 3D, 4D) and K0/K1/K2/K3 are known as width/height/depth/time respectively.

INDEXING (GENERAL CASE)

Once you've allocated space all that remains to know is how to index these arrays, well, let's try to find out how you'd compute the index array:

1d: index = x0
2d: index = x1*k0 + x0
3d: index = x2*k1*k0 + x1*k0 + x0
4d: index = x3*k2*k1*k0 + x2*k1*k0 + x1*k0 + x0
...
Nd: index_n = xn*kn-1*kn-2*...*k0 + index_n-1

The most typical dimensional arrays would be (1D, 2D, 3D, 4D), so usually x0/x1/x2/x3 are known as x/y/z/t respectively.

PARTICULAR CASES (ALLOCATION & INDEXING)

1d: lst = [0]*width
    lst[x]
2d: lst = [0]*width*height
    lst[y*width + x]
3d: lst = [0]*width*height*depth
    lst[z*width*height + y*width + x]
4d: lst = [0]*width*height*depth*duration
    lst[t*width*height*depth + z*width*height + y*width + x]

CONCLUSION

It seems you're dealing with 3d arrays so as explained above we know at this point that one way to allocate a 3d array in python could be done like:

lst = [0]*width*height*depth

and one possible way to index such array (either to write or read it) could be:

lst[z*width*height + y*width + x]

Hope the whole explanation helps to clarify a little bit more about your concerns about arrays, be in python or any other language, the underlying theory will be always the same and the only difference is there may be other more performant ways to allocate/index arrays on those languages, that's all.

๐ŸŒ
Berkeley
pythonnumericalmethods.berkeley.edu โ€บ notebooks โ€บ chapter05.01-For-Loops.html
For-Loops โ€” Python Numerical Methods
You could use the isdigit method of the string to check if the character is a digit. def have_digits(s): out = 0 # loop through the string for c in s: # check if the character is a digit if c.isdigit(): out = 1 break return out