🌐
W3Schools
w3schools.com › python › gloss_python_array_loop.asp
Python Loop Through an Array
You can use the for in loop to loop through all the elements of an array. ... Python Array Tutorial Array What is an Array Access Arrays Array Length Add Array Element Remove Array Element Array Methods ...
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library. Arrays are used to store multiple values in one single variable: ... An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: ... However, what if you want to loop through the cars and find a specific one?
🌐
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.
🌐
W3Schools
w3schools.com › python › python_lists_loop.asp
Python - Loop Lists
For Loops Code Challenge Python Functions · Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range · Range Code Challenge Python Arrays · Arrays Code Challenge Python Iterators ·
🌐
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....
🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
Python Examples Python Compiler ... Interview Q&A Python Bootcamp Python Certificate Python Training ... A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a stri...
🌐
DataCamp
campus.datacamp.com › courses › intermediate-python › loops
Loop over NumPy array | Python
Two NumPy arrays that you might ... the numpy package under the local alias np. Write a for loop that iterates over all elements in np_height and prints out "x inches" for each element, where x is the value in the array....
🌐
W3Schools
w3schoolsua.github.io › python › python_arrays_en.html
Python Arrays. Lessons for beginners. W3Schools in English
Python Arrays. What is an Array? Access the Elements of an Array. The Length of an Array. Looping Array Elements. Adding Array Elements. Removing Array Elements. Array Methods. Python does not have built-in support for Arrays, but Python Lists can be used instead.
🌐
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.
Find elsewhere
🌐
W3Schools
w3schools.com › c › c_arrays_loop.php
C Arrays and Loops
Summary: Always use the sizeof formula when looping through arrays. It makes your loops adapt to the array size automatically. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python iterate over an array
Python Iterate Over an Array - Spark By {Examples}
May 31, 2024 - Here, I will explain with examples of how to loop through every element of the array, loop through by getting index & value, and finally, iterate over a multi-dimensional array using for loop. Following are quick examples of iterating over an array in Python.
🌐
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.
🌐
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:
🌐
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] ...
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
We can also use the enumerate() function to iterate through the list. This method provides both the index (i) and the value (val) of each element during the loop.
Published   December 27, 2025
🌐
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.
🌐
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.
🌐
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.