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
🌐
CodeGym
codegym.cc › java blog › learning python › length of an array in python
Length of an Array in Python
November 7, 2024 - Alternatively, you can write your own function using a for loop to count the elements manually. The simplest and most common method is, of course, using len(). It's efficient, readable, and works with different types of arrays.
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
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
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
April 22, 2023
What's the python version of (i=0;i<array.length;i++)? for x in list doesn't work as expected.

I think this is an XY problem. Explain exactly what you're trying to accomplish and why and how it's not working. for x in y: is the way it ought to be expressed in 99.9% of cases.

More on reddit.com
🌐 r/learnprogramming
27
0
November 10, 2013
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-array-length
Python Array length - GeeksforGeeks
July 23, 2025 - You can get the length by capturing the last index it provides. You must start from 1 to match the actual count. ... Explanation: enumerate(a, 1) loop through the list a with a counter starting at 1. Although the loop does nothing (pass), the ...
🌐
Tutorialspoint
tutorialspoint.com › python › python_loop_arrays.htm
Python - Loop Arrays
We can find the length 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.
🌐
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).
🌐
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.
🌐
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
🌐
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.
🌐
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 - To find the length of an array in Python, you can use the len() function. The len() function in Python returns the length of an object, which is the number of elements present in an object. The syntax for the len() function in Python is as follows:
🌐
CodeRivers
coderivers.org › blog › length-of-an-array-python
Understanding the Length of an Array in Python - CodeRivers
February 22, 2026 - In Python, lists are a versatile data structure that can hold elements of different data types. The length of a list (array) refers to the number of elements it contains. This length is an important property as it helps in determining the size of the data collection and is crucial for operations that need to access or manipulate all the elements.
🌐
Thinkincode
thinkincode.net › python › length-of-array-python
Calculating Array Length in Python | ThinkInCode
# Example my_array = [1, 2, 3, 4, 5] length = 0 for element in my_array: length += 1 print("Length of the array:", length) NumPy is a popular numerical computing library in Python. It provides a numpy array that is more efficient for numerical operations than a regular Python list.
🌐
CodeRivers
coderivers.org › blog › python-length-of-an-array
Python Length of an Array: A Comprehensive Guide — CodeRivers
In this case, the size attribute returns the total number of elements in the NumPy array. For a 2D array, it gives the product of the number of rows and columns. Here, the array has 2 rows and 3 columns, so the size is 6. One common practice is to use the length of an array to loop through its elements.
🌐
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.

🌐
IONOS
ionos.com › digital guide › websites › web development › python list length
How to find the length of a Python list - IONOS
July 18, 2023 - A while loop increases the count with each loop and removes an element from your list (or a copy). A for loop, on the other hand, au­to­mat­i­cal­ly loops every element in your Python list. Here you will need an ad­di­tion­al counter, which you can use to increment in the loop and find out the Python list length.
🌐
Cherry Servers
cherryservers.com › home › blog › cloud computing › how to get the length of a list in python
How to Get the Length of a List in Python | Cherry Servers
November 7, 2025 - Here are some of the most common methods in Python to check the length of a list: The len() methods. Using for loop method (naive method).
🌐
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.
🌐
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
🌐
Trey Hunner
treyhunner.com › 2016 › 04 › how-to-loop-with-indexes-in-python
How to loop with indexes in Python
April 25, 2016 - This first creates a range corresponding to the indexes in our list (0 to len(colors) - 1). We can loop over this range using Python’s for-in loop (really a foreach). This provides us with the index of each item in our colors list, which is the same way that C-style for loops work.
🌐
CodeRivers
coderivers.org › blog › python-length-of-array
Python Length of Array: A Comprehensive Guide — CodeRivers
NumPy arrays are more efficient for working with large datasets and offer various mathematical operations. import numpy as np my_numpy_array = np.array([1, 2, 3, 4, 5]) The most common way to find the length of an array in Python is by using the built-in len() function.