🌐
W3Schools
w3schools.com β€Ί python β€Ί gloss_python_array_loop.asp
Python Loop Through an Array
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 ... You can use the for in loop to loop through ...
🌐
W3Schools
w3schools.com β€Ί python β€Ί python_arrays.asp
Python Arrays
You can use the for in loop to loop through all the elements of an array. ... You can use the append() method to add an element to an array. ... You can use the pop() method to remove an element from the array.
Discussions

Loop over an an array of array
I have an array of arrays I want to loop over to return two arrays called hills and valleys. When looping through each element, we check the item at index 1 that is element[0] if the value is equal to zero, we create another array and push the value inside. we keep a trace of it still when ... More on discuss.python.org
🌐 discuss.python.org
5
0
November 28, 2023
python - loop through array or list in variable steps - Stack Overflow
I'm trying to figure out how to loop through an array or list in variable steps. So for example, if I have the following list... a = [0,0,1,0,0,1,0] ...and want to use the following logic: Star... More on stackoverflow.com
🌐 stackoverflow.com
python - Loop through and create array of objects - Stack Overflow
I'm trying to create a list of objects in Python. The below code should (hopefully) explain what I'm doing: class Base(object): @classmethod def CallMe(self): out = [] #ano... More on stackoverflow.com
🌐 stackoverflow.com
November 21, 2012
Looping through array of objects in Python coming from Javascript - Stack Overflow
Coming from Javascript I am trying to learn Python(very slowly). I'm trying to work out how to do a loop in Python script over an array of objects. For example say I have the following object: cont... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com β€Ί python β€Ί numpy β€Ί numpy_array_iterating.asp
NumPy Array Iterating
Iterating means going through elements one by one. As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python.
🌐
W3Schools
w3schools.com β€Ί python β€Ί python_lists_loop.asp
Python - Loop Lists
Use the len() function to determine the length of the list, then start at 0 and loop your way through the list items by referring to their indexes. Remember to increase the index by 1 after each iteration.
🌐
Tutorialspoint
tutorialspoint.com β€Ί python β€Ί python_loop_arrays.htm
Python - Loop Arrays
The following example shows how ... of array with built-in len() function. Use it to create a range object to get the series of indices and then access the array elements in a for loop....
🌐
Python.org
discuss.python.org β€Ί python help
Loop over an an array of array - Python Help - Discussions on Python.org
November 28, 2023 - When looping through each element, we check the item at index 1 that is element[0] if the value is equal to zero, we create another array and push the value inside. we keep a trace of it still when ...
🌐
NumPy
numpy.org β€Ί doc β€Ί stable β€Ί reference β€Ί arrays.nditer.html
Iterating over arrays β€” NumPy v2.4 Manual
The iterator object nditer, introduced in NumPy 1.6, provides many flexible ways to visit all the elements of one or more arrays in a systematic fashion. This page introduces some basic ways to use the object for computations on arrays in Python, then concludes with how one can accelerate the inner loop in Cython.
🌐
W3Schools
w3schools.com β€Ί python β€Ί python_for_loops.asp
Python For Loops
With the continue statement we ... Yourself Β» Β· To loop through a set of code a specified number of times, we can use the range() function,...
Find elsewhere
🌐
Cach3
w3schools.com.cach3.com β€Ί python β€Ί gloss_python_array_loop.asp.html
Python Loop Through an Array - W3Schools
Python HOME Python Intro Python Get Started Python Syntax Python Comments Python Variables Python Data Types Python Numbers Python Casting Python Strings Python Booleans Python Operators Python Lists Python Tuples Python Sets Python Dictionaries Python If...Else Python While Loops Python For Loops Python Functions Python Lambda Python Arrays Python Classes/Objects Python Inheritance Python Iterators Python Scope Python Modules Python Dates Python JSON Python RegEx Python PIP Python Try...Except Python User Input Python String Formatting
Top answer
1 of 3
3

Don't use a for loop.

for loops in python are different than in C or Java. In those languages, a for loop has an initial condition, a termination condition, and an increment for each time the loop runs. Whereas in python, a for loop is more of a for each loop - you give it an iterable object, and it runs the code for every item in that iterable object.

Modifying the iterable object while you're running through it is a bad idea that can have difficult-to-predict repercussions and will usually break your code.


However, you can always use a while loop:

a = [0,0,1,0,0,1,0]
idx = 0

while(idx < len(a) - 2):
    print(idx)
    if a[idx + 2] == 0:
        idx += 2
    elif a[idx + 2] == 1:
        idx += 1
print(idx)

which produces the expected output

0 1 3 4 6

Or, if you change the increments to 3 and 2 respectively, rather than 2 and 1,

0 2 5
2 of 3
0

Your reasoning is pretty confusing, and I don't see ANY application for this, but here is how I understand your problem...

The reason is because you aren't actually returning the values, you're simply returning the index + 3, which is wrong to begin with. What you're trying to do is point to a new index of the array based on its value and return the index if it contains a value greater than 0.

You need to reference the index you want, check its value, then return the index which contains a value.

a = [0, 0, 1, 0, 0, 1, 0]
for i, v in enumerate(a):
   if i == 0:
      print(i)
      next
   if v == 0:
      next
   else
      print(i)

But let's be honest, this is extremely ugly and un-pythonic. Let's simply check for whether a[i] contains a value, and if so, return the index...

for i, v in enumerate(a):
   if v or i == 0:
      print(i)

The purpose of if v or i == 0 is to check if v has a value, if so, print the index. OR if we are looking at the first element of i.

If you want to EXPLICITLY move the index by two, you must set your index at the start and use a while loop, since enumerate can't help you here and doesn't actually let you move the indicies...

a = [0, 0, 1, 0, 0, 1, 0]
i = 0
while i < len(a) - 1:   # -1 to avoid out of bounds error
   print(i)
   if a[i + 2] == 0:
      i += 2
   elif a[i + 2] == 1:
      i += 1
print(i)            # Final print statement for the last index at the end of the while loop

I want to impress upon you the fact that this solution does NOT scale with different or larger lists, which is why it isn't recommended. By simply checking for whether a value does or doesn't exist, you guarantee your accuracy.

You should simply return the index based upon whether or not it contains a value. Even for very large lists, this will be extremely fast, and will always scale, even for values greater than 1.

The only other reason I would see you would want to do this differently is if you're doing string-search algorithms.

🌐
Dataquest
dataquest.io β€Ί blog β€Ί tutorial-advanced-for-loops-python-pandas
Tutorial: Advanced Python for Loops – Dataquest
March 11, 2025 - A for loop is a programming statement that tells Python to iterate over a collection of objects, performing the same operation on each object in sequence. The basic syntax is: for object in collection_of_objects: # code you want to execute on ...
🌐
Spark By {Examples}
sparkbyexamples.com β€Ί home β€Ί python β€Ί python iterate over an array
Python Iterate Over an Array - Spark By {Examples}
May 31, 2024 - How to use for loop to iterate over an array in Python? Iterate over an array is also referred to as looping through all the elements of an array which can easily perform by using for loops with syntax for x in arrayObj:.
🌐
W3Schools
w3schoolsua.github.io β€Ί python β€Ί python_arrays_en.html
Python Arrays. Lessons for beginners. W3Schools in English
You can use the for in loop to loop through all the elements of an array. ... You can use the append() method to add an element to an array. ... You can use the pop() method to remove an element from the array.
🌐
Trey Hunner
treyhunner.com β€Ί 2016 β€Ί 04 β€Ί how-to-loop-with-indexes-in-python
How to loop with indexes in Python
In Python 2, itertools.izip is equivalent to the newer Python 3 zip function. Here’s a very short looping cheat sheet that might help you remember the preferred construct for each of these three looping scenarios.
🌐
CodeRivers
coderivers.org β€Ί blog β€Ί python-looping-through-array
Python Looping Through Arrays: A Comprehensive Guide - CodeRivers
April 13, 2025 - This blog will explore the different ways to loop through arrays in Python, their usage, common practices, and best practices.
🌐
W3Schools
w3schools.com β€Ί python β€Ί gloss_python_loop_list_items.asp
Python Loop Through List Items
Python Lists Access List Items Change List Items Add List Items Remove List Items Loop Lists List Comprehension Sort Lists Copy Lists Join Lists List Methods List Exercises Code Challenge Python Tuples