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
For the nditer object, this means letting the iterator take care of broadcasting, dtype conversion, and buffering, while giving the inner loop to Cython. For our example, weโ€™ll create a sum of squares function. To start, letโ€™s implement this function in straightforward Python.
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 - 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
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
Looping through array of objects in Python coming from Javascript - Stack Overflow
Find centralized, trusted content ... you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... 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. ... In javascript if I wanted to loop through the array and print to console each name I would do the following. for (let i = 0; ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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:.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-for-loop-array
Python For Loop with Arrays: A Comprehensive Guide - CodeRivers
April 5, 2025 - For example: ... The for loop in ... is: for variable in sequence: # code block to be executed print(variable) Here, the variable takes on each value in the sequence one by one, and the code block inside the loop is executed for each value. To iterate over a list (our "array"), we can use a simple for loop...
๐ŸŒ
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....
๐ŸŒ
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 ...
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.

๐ŸŒ
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).
Published ย  January 2, 2025
๐ŸŒ
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
๐ŸŒ
Dataquest
dataquest.io โ€บ blog โ€บ tutorial-advanced-for-loops-python-pandas
Tutorial: Advanced Python for Loops โ€“ Dataquest
March 11, 2025 - In the code below, we'll write a for loop that iterates through each element by passing z, our two-dimensional array, as the argument for nditer(): ... As we can see, this first lists all of the elements in x, then all elements of y. Remember!
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.5.dev0 Manual
For the nditer object, this means letting the iterator take care of broadcasting, dtype conversion, and buffering, while giving the inner loop to Cython. For our example, weโ€™ll create a sum of squares function. To start, letโ€™s implement this function in straightforward Python.
๐ŸŒ
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 ...
๐ŸŒ
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 - Under the hood, Pythonโ€™s for loop use iterators. Our custom object is now an iterator, and can work with the dunder next method to return successive items in the stream. These two methods work together to enable the iterator protocol. In the initconstructor, we set the index in the object with a value of -1.
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ python-list-loop
7 Ways to Loop Through a List in Python | LearnPython.com
The enumerate() function will help you here; it adds a counter and returns it as something called an โ€˜enumerate objectโ€™. This object contains elements that can be unpacked using a simple Python for loop.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ ways to loop through a list in python
Ways to Loop Through a List in Python - Spark By {Examples}
May 31, 2024 - Then, it uses a while loop to iterate over the elements of the iterator by calling the next() function on the iterator object in each iteration. The next() function returns the next element of the iterator, and the loop continues until there ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ for-loops-in-python
For Loops in Python
February 1, 2020 - For Loop Statements Python utilizes a for loop to iterate over a list of elements. Unlike C or Java, which use the for loop to change a value in steps and access something such as an array using that value.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-create-an-array-for-loop-in-Python
How to create an array for loop in Python - Quora
Answer (1 of 3): If names is an array, you can loop to process it as such: for name in names: # do something # some other thing If you want to filter one list to create new list, use list comprehension. new names = [val for val in names if val != โ€˜ 'โ€™] This will create a new list...
๐ŸŒ
Real Python
realpython.com โ€บ python-for-loop
Python for Loops: The Pythonic Way โ€“ Real Python
February 23, 2026 - You use a for loop to iterate over a list by specifying the loop variable and the list. For example, for item in a_list: allows you to process each item in a_list. Whatโ€™s the difference between an iterable and an iterator in Python?Show/Hide ...