The equivalent of the foreach
statement is actually the python for
statement.
e.g.
>>> items = [1, 2, 3, 4, 5]
>>> for i in items:
... print(i)
...
1
2
3
4
5
It actually works for all iterables in python, including strings.
>>> word = "stackoverflow"
>>> for c in word:
... print(c)
...
s
t
a
c
k
o
v
e
r
f
l
o
w
However, it is worth noting that when using the for loop in this way you are not editing the iterable's values in place as they are a shallow copy.
>>> items = [1, 2, 3, 4, 5]
>>> for i in items:
... i += 1
... print(i)
...
2
3
4
5
6
>>> print(items)
[1, 2, 3, 4, 5]
Instead you would have to use the iterable's index.
>>> items = [1, 2, 3, 4, 5]
>>> for i in range(len(items)):
... items[i] += 1
...
>>> print(items)
[2, 3, 4, 5, 6]