๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_array_loop.asp
Python Loop Through an Array
Python Examples Python Compiler ... Python Bootcamp Python Certificate Python Training ... You can use the for in loop to loop through all the elements of an array....
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_loop_arrays.htm
Python - Loop Arrays
import array as arr # creating ... 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....
๐ŸŒ
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.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python iterate over an array
Python Iterate Over an Array - Spark By {Examples}
May 31, 2024 - By using Python for loop with syntax for x in arrayObj: we can easily iterate or loop through every element in an array. In Python, you have to use the NumPy library to create an array.
๐ŸŒ
StudySmarter
studysmarter.co.uk โ€บ for loop in python
for Loop in Python: Syntax & Range | StudySmarter
How can you traverse multidimensional arrays in Python using a for loop? Use nested for loops to access and manipulate the individual elements within the array, or use the numpy.nditer() method if working with the numpy library. For example: 1) for row in example_array: for item in row: print(item) ...
๐ŸŒ
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.
๐ŸŒ
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. And thankfully, we can use for loops to iterate through those, too.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1684765 โ€บ python-array-for-loop
Python array for loop | Sololearn: Learn to code for FREE!
A for loop runs over the primary elements of a container. So if you wrote: for x in arr: x would be first a tuple of three, then a tuple of two, since that are the primary elements. x, *y now 'unpacks' that primary element, so that you can use ...
Find elsewhere
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
๐ŸŒ
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-program-to-iterate-over-an-array
Python Program to Iterate Over an Array
The original array is: [1, 2, 3, 4, 5, 6] Iterate Over an Array: 1 2 3 4 5 6 ยท Note: The while loop will become an infinite loop if we forgot to increase the index value (i += 1). Enumerate() is a python built-in function, it takes an iterable-like array and returns a tuple containing a count and the values obtained from iterating over an iterable.
๐ŸŒ
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...
๐ŸŒ
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.
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ python-list-loop
7 Ways to Loop Through a List in Python | LearnPython.com
Although weโ€™ve used for num in nums: for its simplicity in this example, itโ€™s usually better to use for num in np.nditer(nums): when youโ€™re working with large lists. The np.nditer function returns an iterator that can traverse the NumPy array, which is computationally more efficient than using a simple for loop. Python loops are useful because they allow you to repeat a piece of code.
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter05.01-For-Loops.html
For-Loops โ€” Python Numerical Methods
Just like if-statements, for-loops can be nested. EXAMPLE: Let x be a two-dimensional array, [5 6;7 8]. Use a nested for-loop to sum all the elements in x.
๐ŸŒ
Kompulsa
kompulsa.com โ€บ home โ€บ python tutorials: construct a for loop in python
Python Tutorials: How To Construct For Loops In Python
November 1, 2023 - If you want it to execute 5 times, change โ€˜range(10)โ€™ to โ€˜range(5)โ€™. Notice that I left an empty line after the last line of code, which you should do when writing Python. ... You can also make it loop until you have reached the end of an array. The example array used below โ€˜testarrayโ€™, has three members, so it will loop 3 times. It uses the โ€˜len()โ€™ function to retrieve the length of the array. ... testarray = ['cat', 'bird', 'orange'] for i in range(len(testarray)): print('This string will be displayed 3 times because there are three members in testarray')
๐ŸŒ
Studytonight
studytonight.com โ€บ python โ€บ looping-in-python
Python Loops - for Loop and while Loop | Studytonight
The for loop, is frequently used ... a list too! The logic is pretty much similar for accessing an element using indices. Run the loop from 0 to len(myList) and simply access the elements....