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 Overflowpython - Creating for loop until list.length - Stack Overflow
Loop over array elements without knowing length
Why iterate over an array using the index?
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.
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
Yes you can, with range [docs]:
for i in range(1, len(l)):
# i is an integer, you can access the list element with l[i]
but if you are accessing the list elements anyway, it's more natural to iterate over them directly:
for element in l:
# element refers to the element in the list, i.e. it is the same as l[i]
If you want to skip the the first element, you can slice the list [tutorial]:
for element in l[1:]:
# ...
can you do another for loop inside this for loop
Sure you can.
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.