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 ... Bootcamp Python Certificate Python Training ... You can use the for in loop to loop through all the elements of an array....
๐ŸŒ
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
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
0
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... ... 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
Why iterate over an array using the index?
You do it if there is something in your loop that will use an index rather than the elements of the array. For instance, sometimes you want to sample something from another array that is not your loop array A cleaner way to do it is by using enumerate function for i, item in enumerate(array): The index gets assigned to i and whatever element of the array gets assigned to item. This may also be a bad habit from Matlab, people that come from Matlab are more used to looping over indexes. More on reddit.com
๐ŸŒ r/learnpython
57
46
April 22, 2023
๐ŸŒ
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:.
๐ŸŒ
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 ...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-iterate-over-an-object-in-Python
How to iterate over an object in Python - Quora
Answer (1 of 5): You don't. Python objects are not as simple as what JavaScript conflates between "objects" and (associative) "arrays." An object in Python may have an associated __dict__ attribute. This is the default case for objects defined via the class keyword (and it's associated syntacti...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.1 Manual
Since the Python exposure of nditer is a relatively straightforward mapping of the C array iterator API, these ideas will also provide help working with array iteration from C or C++. The most basic task that can be done with the nditer is to visit every element of an array.
Find elsewhere
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 5 best ways to iterate over an array in python
5 Best Ways to Iterate Over an Array in Python - Be on the Right Side of Change
February 26, 2024 - The map() function applies a given function to every item of an iterable (like an array) and returns a list of the results. ... The map() function in this code uses a lambda function that returns each element as-is, essentially iterating over ...
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.

๐ŸŒ
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 - The CustomIterTeams object, prem_teams ... iterate through. The index is deliberately re-set to its original value once the index reaches the length of the list, before a StopIteration exception is raised. This feature is implemented in order for the user to perform multiple iterations of the object if they want to in the same session, as shown in the python prompt shown ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Python Pool
pythonpool.com โ€บ home โ€บ blog โ€บ 11 powerful methods to iterate through list in python
11 Powerful Methods to Iterate Through List in Python
January 1, 2024 - Basically in layman language, it converts the dimensions of the array-like in this example we have used the reshape() function to make out numpy array a 2D array. ... The fourth way in our list is iteration using the enumerate method. If you donโ€™t know what enumerate exactly do in python, then let me explain to you. The enumerate() method adds counter to an iterable and returns it. And whatever the enumerate method returns, it will be a enumerate object...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_loop_arrays.htm
Python - Loop Arrays
When you are using this loop with arrays, initialize a loop variable before entering the loop. This variable often represents an index for accessing elements in the array. Inside the while loop, iterate over the array elements and manually update the loop variable. The following example shows how you can loop through an array using a while loop โˆ’
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
Here we are using a while loop to iterate through a list. We first need to find the length of list using len(), then start at index 0 and access each item by its index then incrementing the index by 1 after each iteration.
Published ย  December 27, 2025
๐ŸŒ
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
To start, we need to establish the middle point of our array. Why the middle, you ask? Well, our task necessitates that we traverse the array from the center to the ends. Python's integer division operator,
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.2 Manual
Since the Python exposure of nditer is a relatively straightforward mapping of the C array iterator API, these ideas will also provide help working with array iteration from C or C++. The most basic task that can be done with the nditer is to visit every element of an array.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python iterate over list
Python Iterate Over List - Spark By {Examples}
May 31, 2024 - Use for item in list syntax to iterate over a list in Python. By using this for loop syntax you can iterate any sequence objects
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ python-list-loop
7 Ways to Loop Through a List in Python | LearnPython.com
Ending while loops properly is ... loops in Python here. The methods weโ€™ve discussed so far used a small lists. However, efficiency is essential when youโ€™re working with larger amounts of data. Suppose you have large single-dimensional lists with a single data type. In this case, an external library like NumPy is the best way to loop through big lists. NumPy reduces the overhead by making iteration more efficient. This is done by converting the lists into NumPy arrays...