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. Answer from waspbr on reddit.com
🌐
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....
🌐
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.
Discussions

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
October 5, 2022
python - Iterating through a for loop with the size of an array - Stack Overflow
I want my program to be able to print out the sum of all numbers below n that are divisible by 3 and also 5. #!/bin/python3 import sys import math arr = [] arr3 = [] arr5 = [] tn = 1 sum1 = 0 sum... 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
Assign values to array during loop - Python - Stack Overflow
I would like to write a loop in Python, where the size of the array increases with every iteration (i.e., I can assign a newly calculated value to a different index of a variable). For the sake of this question, I am using a very simple loop to generate the vector t = [1 2 3 4 5]. In Matlab, ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Tutorialspoint
tutorialspoint.com β€Ί python β€Ί python_loop_arrays.htm
Python - Loop Arrays
The for loop is used when the number of iterations is known. If we use it with an iterable like array, the iteration continues until it has iterated over every element in the array.
Top answer
1 of 4
3

Your solution seems correct - but it seems u have a couple of syntax errors if you're using python You also have to re-initialize sum1 & sum2 between test cases. Right now you get the total sum of all arrs rather than arr[x]-hopefully that makes sense. You also have to remove duplicate numbers from the sum. For instance, if you're getting the sum of all numbers up to 20 - you'll end up adding 15 twice as it is divisible by 3 and 5. So the inner for loops will add it twice to the sum. So you'll need to remove 15 to get rid of duplicates.

for x in range(len(arr)): #by default range starts at o. therefore, range(len(arr)) = range(0, len(arr)) 
    sum1 = 0 # you forgot to initialize sum1
    sum2 = 0 # you forgot to initialize sum2
    duplicates = 0 #you have to remove duplicates from the answer
    for b5 in range (0, arr[x], 5): #you have to add the colons here
        sum1 = sum1 + b5
    for b3 in range (0, arr[x], 3): #you have to add the colons here
        sum2 = sum2 + b3
    for dup in range(0, arr[x], 3*5): # removes duplicates from the final sum
        duplicates = duplicates + dup
    sum = sum1 + sum2 - duplicates 
    print(sum)

This is an O(n^2) solution - you can drop it down to O(n) using a little bit of math.

You'll notice that the inner for loops can be represented using the formula sum(n)=Ξ£d*i=d*Ξ£i-where the summation starts at i = 0, end at ⌊(n-1)/dβŒ‹ and d is the divisor (in the case of your question d=3 or 5).

for b5 in range (0, arr[x], 5):
    sum1 = sum1 + b5

(https://en.wikipedia.org/wiki/Summation)

There is a very common summation formula that is commonly used to convert summations into a closed-form expression (something with finite steps - which is O(1))

Ξ£i=n*(n+1)/2

In the case of the inner loop - it would be sum(n) = d*(⌊(n-1)/dβŒ‹)*(⌊(n-1)/dβŒ‹+1)/2.

let,

f(n,d) = (⌊(n-1)/dβŒ‹+1)/2

Therefore, the solution to your problem would be f(n,3)+f(n,5)-f(n,3*5)

Which would convert the inner for loops from O(n) to O(1). Which, means your entire solution would be O(n).

I'll let you figure out the code on your own. However, theoretically, there is a better solution; such that as arr grows indefinitely the work scales linearly rather than quadratically.

2 of 4
0
  1. Create Array with input number (Eg. arr = [12, 15, 4] )
  2. Iterate over the created Array
  3. In for loop check if number are divisible by 3 and 5

Sample code:

    arr = [12, 15, 4]
    total = 0
    
    for num in arr:
        if num % 3 == 0 and num % 5 == 0:
            total = total + num
    
    print(total) # 15
🌐
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.
🌐
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 we meet another element where the element[0] ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
We can use the range() method with for loop to traverse the list. This method allow us to access elements by their index, which is useful if we need to know the position of an element or modify the list in place.
Published Β  December 27, 2025
🌐
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! When looping through these different data structures, dictionaries require a method, NumPy arrays require a function. When we're working with data in Python, we're often using pandas DataFrames.
🌐
Spark By {Examples}
sparkbyexamples.com β€Ί home β€Ί python β€Ί python iterate over an array
Python Iterate Over an Array - Spark By {Examples}
May 31, 2024 - # Iterate by getting index and value for index, value in np.ndenumerate(arr): print(index, value) Yields below output. ... If you have a multi-dimensional array you need to perform multiple loops, to overcome this problem use nditer function of the NumPy module in Python.
🌐
W3Schools
w3schools.com β€Ί python β€Ί numpy β€Ί numpy_array_iterating.asp
NumPy Array Iterating
As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python.
🌐
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.
🌐
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...
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu β€Ί notebooks β€Ί chapter05.01-For-Loops.html
For-Loops β€” Python Numerical Methods
EXAMPLE: Let x be a two-dimensional array, [5 6;7 8]. Use a nested for-loop to sum all the elements in x.
🌐
Sololearn
sololearn.com β€Ί en β€Ί Discuss β€Ί 1684765 β€Ί python-array-for-loop
Python array for loop | Sololearn: Learn to code for FREE!
Came across this for loop in a python challenge. I'm still trying to understand exactly what it's doing. Any help? arr = [(5,8,0), (2,4)] sum = [] for x, *y in arr:
🌐
Studytonight
studytonight.com β€Ί python β€Ί looping-in-python
Python Loops - for Loop and while Loop | Studytonight
In the second code snippet, we will be using a loop, where we will only have to write whatever tedious task we have to achieve, only once, and then put it on a loop. With this, we will be able to achieve an iterative flow for execution. In python, there are two ways to achieve iterative flow: ... for [ELEMENT] in [ITERATIVE-LIST]: [statement to execute] else: [statement to execute when loop is over] [else statement is completely optional]