In Python you can iterate over the list itself:

for item in my_list:
   #do something with item

or to use indices you can use xrange():

for i in xrange(1,len(my_list)):    #as indexes start at zero so you 
                                    #may have to use xrange(len(my_list))
    #do something here my_list[i]

There's another built-in function called enumerate(), which returns both item and index:

for index,item in enumerate(my_list):
    # do something here

examples:

In [117]: my_lis=list('foobar')

In [118]: my_lis
Out[118]: ['f', 'o', 'o', 'b', 'a', 'r']

In [119]: for item in my_lis:
    print item
   .....:     
f
o
o
b
a
r

In [120]: for i in xrange(len(my_lis)):
    print my_lis[i]
   .....:     
f
o
o
b
a
r

In [122]: for index,item in enumerate(my_lis):
    print index,'-->',item
   .....:     
0 --> f
1 --> o
2 --> o
3 --> b
4 --> a
5 --> r
Answer from Ashwini Chaudhary on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-array-length
Python Array length - GeeksforGeeks
July 23, 2025 - Explanation: sum(1 for _ in a) add 1 for each element in the list a, effectively counting the total number of items. You manually loop through each element and increment a counter variable.
Discussions

python - Creating for loop until list.length - Stack Overflow
I'm reading about for loops right now, and I am curious if it's possible to do a for loop in Python like in Java. Is it even possible to do something like for (int i = 1; i More on stackoverflow.com
🌐 stackoverflow.com
Defining python array size for a loop - Stack Overflow
I need to add data on each row of a array through a for loop. For that i thought of using vstack. but since i have two types of array (different in size) I want to define the column numbers, only ... More on stackoverflow.com
🌐 stackoverflow.com
finding the size of an array in python - Stack Overflow
This for loop basically makes a cell array for the 83 trials I have. The issue im having is that when I use len for the cell array. i get the array with the length of the individual arrays. More on stackoverflow.com
🌐 stackoverflow.com
Loop over array elements without knowing length
Either your array is static or it's dynamic. If the array is static you know its length even if you don't explicity set it to a known length. You can do this int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; const int ARRAYLEN = (sizeof(array) / sizeof(array[0])); Use the ARRAYLEN value in your code. If the array is dynamic (malloc(), etc) you need to keep track of the array size, there's no other way. More on reddit.com
🌐 r/arduino
22
10
September 6, 2024
🌐
Naiveskill
naiveskill.com › home › determine python array length | complete tutorial with examples in 2023
Determine python array length | Complete tutorial with examples in 2023 - Naiveskill
January 4, 2023 - since my_array is not empty, so we get “The array is not empty” as output. For loop can be used with len() function to loop over the elements of an array using their indices.
🌐
FavTutor
favtutor.com › blogs › find-length-of-array-python
How to Find Length of an Array in Python? (5 Best Methods)
September 19, 2022 - Let's first try out finding the length of a list using the for a loop. Note that the variable 'length' is initialized to 0. Its value increases as the loop go through each element of the array (list). #Find length of a List in Python #Create a List a = [10, 20, 30, 40, 50] #create a variable to hold the length length = 0 for element in a: length += 1 print("The length of the array", a, " is: ", length)
🌐
CodeGym
codegym.cc › java blog › learning python › length of an array in python
Length of an Array in Python
November 7, 2024 - ... Pretty straightforward, right? You simply pass the array or list to len(), and it gives you the count of elements. Another way to determine the length of an array is by using a for loop...
🌐
iO Flood
ioflood.com › blog › get-length-of-array-in-python-guide-and-examples
Get Length of Array in Python: Guide and Examples
March 12, 2024 - As such, it’s generally recommended to use len() instead. An alternative method to calculate the length of an array involves using a for loop to iterate over the array and count the number of elements.
🌐
Freakyjolly
freakyjolly.com › how-to-get-array-length-using-for-loop-in-python
How to Get Array Length Using For Loop in Python?
February 13, 2022 - We have a list of type array as ... list_count += 1 print ("List length count = ", list_count) #OUTPUT List length count = 4 · Ideally, we can use the len() function in Python to get the length of list items as shown below:...
Find elsewhere
🌐
Liberian Geek
liberiangeek.net › home › how-to/tips › get the length of an array in python
Get the Length of an Array in Python | Liberian Geek
June 13, 2026 - In the Python programming language, various approaches can be utilized to compute the length of Python arrays. To get the specific array’s length, users can use the “len()” method, “length_hint()” method, “__len__()” method, “.size” or “.shape” attribute of NumPy library or “for” loop.
🌐
freeCodeCamp
freecodecamp.org › news › python-length-of-list-how-to-find-the-size-of-a-list
Python Length of List – How to Find the Size of a List
January 31, 2023 - Then we have the for loop statement, where we loop through each value in the array. In each loop, we increment the size variable by 1. In the end, the size variable will hold the size of the list. Let's look at the short way next.
🌐
DataCamp
datacamp.com › home › tutorials › python › python list size: 8 different methods for finding the length of a list in python
Python List Size: 8 Different Methods for Finding the Length of a List in Python | DataCamp
February 7, 2024 - # Naive method using a for loop to count the list's size counter = 0 for item in my_list: counter += 1 print(counter) # Output: 4 ... List comprehensions in Python offer a compact syntax for performing operations on a list.
🌐
Tutorialspoint
tutorialspoint.com › python › python_loop_arrays.htm
Python - Loop Arrays
import array as arr # creating ... display the following output − ... We can find the length of array with built-in len() function....
🌐
Stack Overflow
stackoverflow.com › questions › 31797428 › defining-python-array-size-for-a-loop
Defining python array size for a loop - Stack Overflow
Array=[] # bad name, especially when the value is a list forArray=[] for line in searchlines: Array=re.findall(r'[-+]?\d*\.\d+|\d+', line) # why did you intially set Array to []? size=len(Array) if size == 3: [G,x,y]= Array # so you are assigning the 3 values in Array to 3 variables if size == 5: [G,x,y,i,j]= Array # and 5 variables forArray=np.vstack((forArray,Array))
🌐
IONOS
ionos.com › digital guide › websites › web development › python list length
How to find the length of a Python list - IONOS
July 18, 2023 - With this loop, you don’t need the pop() function since the elements in the list do not need to be deleted. l = ["python", "is", "a", "versatile", "language"] count = 0 for element in l: count += 1python
🌐
Stack Overflow
stackoverflow.com › questions › 69857930 › finding-the-size-of-an-array-in-python
finding the size of an array in python - Stack Overflow
for i in set(data['MovementNumber'].values): data2 = data.loc[data['MovementNumber'] == i] x = len(data2) print(x) This for loop basically makes a cell array for the 83 trials I have. The issue...
🌐
Reddit
reddit.com › r/arduino › loop over array elements without knowing length
r/arduino on Reddit: Loop over array elements without knowing length
September 6, 2024 -

How do I write a for loop that goes through all the elements of an array in a way that ensures that the length is respected?

If I already know what the length is, I can do something like:

int array[5] {1, 2, 3, 4, 5}
for (int i = 0; i < 5; i++){
  println(array[i]);
}

But what if I don't know the length, or don't want to manually keep track? Can I do something like the python

for element in array:
  print(element)

or

for i in range(len(array)):
  print (array(i))

It looks like Arduino has the sizeof() command, but that seems to be getting a size in bytes, rather than a "number of elements", which isn't useful for this purpose if you're reading off a bunch of long floats.

🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_array_size.htm
NumPy - Array Size
When you need to iterate through each element in an array, knowing the size helps you define the range for your loop −
🌐
W3Schools
w3schools.com › python › gloss_python_array_length.asp
Python Array Length
Python Array Tutorial Array What is an Array Access Arrays Looping Array Elements Add Array Element Remove Array Element Array Methods ... Coding fundamentals as bite-sized lessons and challenges.
🌐
GeeksforGeeks
geeksforgeeks.org › python-array-length
Python Array length - GeeksforGeeks
May 8, 2025 - Explanation: sum(1 for _ in a) add 1 for each element in the list a, effectively counting the total number of items. You manually loop through each element and increment a counter variable.
🌐
Python Guides
pythonguides.com › check-the-length-of-an-array-in-python
How To Check The Length Of An Array In Python?
July 22, 2026 - For a list or array.array, use if not my_array:. For a NumPy array, use my_array.size == 0 because direct truth checks can become ambiguous. len() is Python’s built-in function for counting top-level items.