There are several constructs you can use. The one that comes closest to the foreach of other languages is
a = {1, 2, 3};
Do[Print[i], {i, a}]
Note that unlike in Python, the iterator variable i is local to Do.
There's an analogous Table syntax. In Mathematica, we use Table much more frequently than Do.
Map and Scan are also useful alternatives to Table and Do, for example Scan[Print, a].
Further variants of these are discussed in Applying Functions to Lists.
Finally, many functions in Mathematica have the Listable attribute, which means that they automatically Map over lists:
SetAttributes[f, Listable]
f[{1, 2, 3}]
(* {f[1], f[2], f[3]} *)
To be more precise, they automatically MapThread over sets of lists:
f[{1, 2, 3}, {a, b, c}]
(* {f[1, a], f[2, b], f[3, c]} *)
Map[]andTable[], no? Additionally, a lot of built-in functions areListable, so that level of indirection isn't even needed. – J. M.'s missing motivation Sep 12 '17 at 01:36Do:a = {1, 2, 3}; Do[Print[i], {i, a}]. Python list comprehensions can be translated usingTable. As @J.M. says, usingMapand listability, this type of loop is often not necessary in Mathematica. – Simon Rochester Sep 12 '17 at 02:19Scan[]:Scan[Print, a]. – J. M.'s missing motivation Sep 12 '17 at 02:20a = [4,5,6] x = range(len(a)) for i in x: print a[i]
– Rene Duchamp Sep 12 '17 at 12:04