for foo in some_dict iterates through the keys of a dictionary, not its values.

d = {'a': 1, 'b': 2, 'c': 3}
for dd in d:
    print(dd)
# gives a; b; c

You probably want to do for foo in some_dict.values()

for dd in d.values():
    print(dd)
# gives 1; 2; 3
Answer from Adam Smith on Stack Overflow
๐ŸŒ
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 ...
๐ŸŒ
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.
Discussions

python - Iterate over dictionary of objects - Stack Overflow
I have a dictionary of objects which contains the "Names/Ranges" within a spreadsheet. As I process the spreadsheet I need to update the value associated with a range. The class to hold ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
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
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... ... However, I'm not quite clear how I can implement this logic as it seems like I can't change my index value I iterate through. Why does this snippet still return values from 0-6 instead of 0,3,6? ... Well, range objects ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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 ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_loop_arrays.htm
Python - Loop Arrays
import array as arr newArray = ... condition is true. When you are using this loop with arrays, initialize a loop variable before entering the loop....
๐ŸŒ
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:.
Find elsewhere
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 ...
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ applying-simple-looping-in-practice-with-python โ€บ lessons โ€บ iterating-through-an-array-from-middle-to-ends
Iterating Through an Array from Middle to Ends
Here's an illustration of how we can implement this in our Python function: ... Now that our pointers are initialized, it's time to traverse the array and construct our new order. For this, we can effectively utilize a while loop in Python, which continues looping until the left pointer is ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.1 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.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ how to loop through your own objects in python
How to Loop Through Your Own Objects in Python | Towards Data Science
March 5, 2025 - This feature is implemented in ... in the python prompt shown below. We can also now reverse the ordering of the teams, by simply implementing the dunder reserved method. It is not necessary to define a dunder next method in order to make a user-defined object iterable. Rather, we get just get the dunder iter method to return a generator, that loops through our ...
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.5.dev0 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.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-looping-through-array
Python Looping Through Arrays: A Comprehensive Guide - CodeRivers
April 13, 2025 - Looping through arrays is an essential skill in Python programming. Whether you use for loops, while loops, or take advantage of functions like enumerate and zip, understanding the different methods and their use cases can greatly improve your code's readability and efficiency.
๐ŸŒ
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-program-to-iterate-over-an-array
Python Program to Iterate Over an Array
May 15, 2023 - This is the easiest method to iterate an array in python programming. Here the for loop iterates all the array elements. In this example, The variable ele is the loop variable it stores the element corresponding to the each iteration. Rather than using the index number to accasses one by one element, here we can simply loop through and disply the array elements in the output.
๐ŸŒ
Trey Hunner
treyhunner.com โ€บ 2016 โ€บ 04 โ€บ how-to-loop-with-indexes-in-python
How to loop with indexes in Python
Now letโ€™s talk about loops in Python. First weโ€™ll look at two slightly more familiar looping methods and then weโ€™ll look at the idiomatic way to loop in Python. If we wanted to mimic the behavior of our traditional C-style for loop in Python, we could use a while loop:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
It provides the shortest syntax for looping through list. ... a = [1, 3, 5, 7, 9] # On each iteration val is passed to print function # And printed in the console. [print(val) for val in a] ... Note: This method is not a recommended way to iterate through lists as it creates a new list (extra space). ... In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of ...
Published ย  January 2, 2025
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ looping-through-array-in-python
Looping Through Arrays in Python: A Comprehensive Guide - CodeRivers
April 25, 2025 - In Python, there is no built - ... types and can grow or shrink in size. ... The most common way to loop through an array (list) in Python is using a for loop....