If you are going for a literal translation from Python to Mathematica, I think that something like the following snippet should do the trick:
Part[
Reap@Do[
Do[
If[
MemberQ[a, r],
Sow@Flatten[{a, r}]
], {r, R}], {a, A}], 2, 1]
where A = {{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}
and R=Range[1,3]
(note how in Python $list(range(1, 4))=[1, 2, 3]$)
Nevertheless, this question is about finding "...the most natural way to translate this Python code to Mathematica" and my view is that going for the literal translation is not always the best option (although it could very possibly be what you need at a certain point in time).
The answers to this question provide useful examples of how versatile Mathematica is.
Another natural way to achieve the intended result, is to use Map
:
Flatten[
Map[(
a = #;
Flatten[{a, #}] & /@ Pick[R, MemberQ[a, #] & /@ R]
) &, A], 1]
Perhaps, a more concise way to achieve the same result is the following:
f[a_, r_] := If[MemberQ[a, r], Flatten[{a, r}]]
Cases[
Flatten[
Outer[f, A, R, 1], 1], Except[Null], 1]
I think that this way is more close in practice to what the list comprehension does: it traverses the items of A
(rows) and then it traverses the items in the range until it finds a match.Then it appends the range item to the list row and proceeds to the next row.