# List comprehensions in Mathematica?

I'm looking for an implementation of list comprehensions in Python (nested for-in statement with destructuring and conditionals). Here's an example:

matrix=[['a','b'],['c','d']]
flat=[(el,i,j) for (i, array) in enumerate(matrix) for (j, el) in enumerate(array) if i!=j]
print(flat) Now, anyone could write this with a table or loop, but that's not what I'm looking for.

• I don't understand what the Python code does and, frankly, I don't really think you are providing enough info here. Can you explain what you want to do, or perhaps provide the corresponding table or loop MMA code that you mention? What functionality are you looking for exactly? – MarcoB Jul 2 at 17:51
• That kind of functionality is provided by Dataset. – Anton Antonov Jul 2 at 18:11
• @MarcoB This is probably the best/most-used feature of python. I'm just looking for an implementation of something equally useful in mma. – M.R. Jul 2 at 20:08
• Well, one might say that this is not a python site, and make allowances to encourage folks who know Mathematica well, which is what is needed for a good answer, to try to help. After all, it's not hard to summarize the functionality in the question. Further, re "that's not what I'm looking for": what are you looking for? The iterators in python don't have exact equivalents in M, however close MapIndexed is to for (i, array) in enumerate(matrix). And since you have to use a nested loop in python, how do you expect not to have to use a loop in M? – Michael E2 Jul 2 at 22:59
• Why not just MapIndexed with a level spec of {2} and a conditional Nothing and then call Flatten[#, 1] on that? Seems to me to be the easiest way. – b3m2a1 Jul 3 at 0:32

IMO, Mathematica's functional operators more than cover the use cases for list comprehensions in Python. There are a couple of different list comprehension syntaxes which are covered by a couple of different functions in Mathematica:

[f(v) for v in values] is covered by Map[f, values]

[f(v, i) for v in enumerate(values)] is covered by MapIndexed[f, values]

[f(v1, v2) for v1, v2 in zip(values1, values2)] is covered by MapThread[f, {values1, values2}]

The syntax [f(v) for v in values if v == x] is typically accomplished with Select or Pick, as in Select[f /@ values, # == x &].

Python list comprehensions can also be used for nested loops:

[f(x, y) for x in xvalues
for y in yvalues]


This is equivalent to Flatten@Outer[f, xvalues, yvalues].

Mathematica's functionality in this area is much better than Python's list comprehension. Mathematica's functions are more readable and more flexible, for example in cases where you need to use the level argument.

• f/@Select[values,#==x&] matches more closely – Philip Maymin Jul 3 at 11:06

Something like this?

matrix = {{"a", "b"}, {"c", "d"}};

allflat = Flatten[MapIndexed[List, matrix, {2}], 1]
(*    {{"a", {1, 1}}, {"b", {1, 2}}, {"c", {2, 1}}, {"d", {2, 2}}}    *)

flat = Select[allflat, Apply[Unequal]@*Last]
(*    {{"b", {1, 2}}, {"c", {2, 1}}}    *)

• You can also use Nothing and a condition to obviate the need for the Select after the fact. – b3m2a1 Jul 3 at 0:33

A straightforward translation of a general list comprehension, suggested by various Python tutorials, even if not the most efficient way (see for instance @Roman's answer), is to append each item to a list:

Module[{res = {}},
MapIndexed[  (* use Do[], Map[], or MapIndexed[] to implement the iterable *)
Function[{array, i},
MapIndexed[
Function[{el, j},
If[i != j,                               (* if condition *)
AppendTo[res, {el, First@i, First@j}]]  (* add to list *)
],
array
]
],
matrix
];
res
]


Reap and Sow might be the closest thing in Mathematica to a list comprehension, and effectively implements the above approach.

Reap[
MapIndexed[
Function[{array, i},
MapIndexed[
Function[{el, j},
If[i != j, Sow[{el, First@i, First@j}]]
],
array
]
],
matrix
]
][[2, 1]]


And Reap-Sow allows multiple list comprehensions simultaneously with the second tag argument.

Note: Sometimes one can use Table[] with If[condition, x, Nothing] to implement [x for x in list if condition]. But implementing nested iterations with Table[], such as in the OP's example, would result in nested lists instead of a flat list. They could be flattened. For the OP's example:

Flatten[
Table[
With[{array = matrix[[i]]},
Table[
With[{el = array[[j]]},
If[i != j, {el, i, j}, Nothing]
],
{j, Length@array}
]
],
{i, Length@matrix}
],
1
]


Note that with Table[], Do[], and Map[] you can only have one item, either the index j or the element el. If you get the index, you can extract the element as above.. To get both at once, you would have to use MapIndexed. Note also that instead of Table[], you can use MapIndexed at level 2, as in @Roman's answer, which produces a nested, non-flat, result. Using MapIndexed[] instead of Table[] above gives us the following solution:

Flatten[
MapIndexed[
If[Unequal @@ #2, Flatten[{##}], Nothing] &,
matrix,
{2}],
1]


...an implementation of list comprehensions in Python

We could implement Python (poorly) in Mathematica:

Needs@"GeneralUtilities";

ClearAll[for];
SetAttributes[for, HoldAll];
for[x_ ∈ iterator_, body_] /; MatchQ[iterator, _Iterator] :=
Module[{i, iter = iterator, tag},
Hold[x] /. Hold[{v___}] | Hold[v___] :>
Block[{v},    (* Blocks variables in x *)
Reap[
While[
i =!= IteratorExhausted,
x = i;
Sow[body, tag]
],
tag][[2, 1]]
]];

enumerate[list_List] := NewIterator[
enumerate,
{i = 0, max = Length@list},
If[i++ < max, {i, list[[i]]}, IteratorExhausted]];

ClearAll[lc];
SetAttributes[lc, HoldAll];
lc[x_, iterFN_] := Module[{tag},
Reap[iterFN[Unevaluated@Sow[x, tag]], tag][[2, 1]]
];


Now the syntax is pretty close to Python's:

[
(el,i,j)
for (i, array) in enumerate(matrix) for (j, el) in enumerate(array) if i!=j
]

lc[
{el, i, j},
for[{i, array} ∈ enumerate@matrix, for[{j, el} ∈ enumerate@array, If[i != j, #]]] &
]

(*  {{"b", 1, 2}, {"c", 2, 1}}  *)


One could even alter enumerate[] to index arrays from 0 instead of 1.

You can implement a do notation in Mathematica.

First, implement functions for list monad:

bind[fa_, f_] := Flatten[f /@ fa, 1]
return[a_] := {a}
guard[True] := {tt}
guard[False] := {}


And do notation:

do[to[x_, fa_], fb__] := bind[fa, x \[Function] do[fb]]
do[to[xs_List, fa_], fb__] := bind[fa, (xs \[Function] do[fb]) @@ # &]
do[fa_, fb__] := bind[fa, do[fb] &]
do[fa_] := fa
SetAttributes[do, HoldAll]


Then you can use it like this:

enumerate[l_] := Table[{i, l[[i]]}, {i, Length@l}]

matrix = {{"a", "b"}, {"c", "d"}}

do[
{i, array}~to~enumerate[matrix],
{j, el}~to~enumerate[array],
guard[i != j],
return[{el, i, j}]
]

• +1. But what if someone sets tt = Sequence[0, 1]? – Michael E2 Aug 26 at 15:46
• @MichaelE2 That's a problem, you can use True instead or protect the symbol tt`. – wul Aug 27 at 7:47