6
$\begingroup$

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)

enter image description here

Now, anyone could write this with a table or loop, but that's not what I'm looking for.

$\endgroup$
5
  • 6
    $\begingroup$ 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? $\endgroup$
    – MarcoB
    Commented Jul 2, 2019 at 17:51
  • $\begingroup$ That kind of functionality is provided by Dataset. $\endgroup$ Commented Jul 2, 2019 at 18:11
  • 1
    $\begingroup$ @MarcoB This is probably the best/most-used feature of python. I'm just looking for an implementation of something equally useful in mma. $\endgroup$
    – M.R.
    Commented Jul 2, 2019 at 20:08
  • 2
    $\begingroup$ 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? $\endgroup$
    – Michael E2
    Commented Jul 2, 2019 at 22:59
  • $\begingroup$ 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. $\endgroup$
    – b3m2a1
    Commented Jul 3, 2019 at 0:32

5 Answers 5

16
$\begingroup$

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.

$\endgroup$
4
  • 3
    $\begingroup$ f/@Select[values,#==x&] matches more closely $\endgroup$ Commented Jul 3, 2019 at 11:06
  • $\begingroup$ "Mathematica's functionality in this area is much better than Python's list comprehension" why, when, where, how? $\endgroup$ Commented Jan 13, 2021 at 16:26
  • 1
    $\begingroup$ @opisthofulax It’s not even a competition, really. For example, you can’t set the level in Python list comprehensions like you can in all the functions I mentioned above. Add to that the functions that support patterns and Mathematica simply blows Python out of the water. $\endgroup$
    – C. E.
    Commented Jan 13, 2021 at 17:24
  • $\begingroup$ This answer is so useful to me in the opposite direction, i.e. learning Python after I already know Mathematica. (I would love to see more on this. I have not been able to find a good Python for MMA programmers reference yet, see my question mathematica.stackexchange.com/questions/238433/…) $\endgroup$
    – Kvothe
    Commented Jul 28, 2021 at 14:37
7
$\begingroup$

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}}}    *)
$\endgroup$
1
  • 1
    $\begingroup$ You can also use Nothing and a condition to obviate the need for the Select after the fact. $\endgroup$
    – b3m2a1
    Commented Jul 3, 2019 at 0:33
7
$\begingroup$

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]
$\endgroup$
5
$\begingroup$

...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 = Read[iter];
         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.

$\endgroup$
4
$\begingroup$

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}]
 ]
$\endgroup$
2
  • 1
    $\begingroup$ +1. But what if someone sets tt = Sequence[0, 1]? $\endgroup$
    – Michael E2
    Commented Aug 26, 2019 at 15:46
  • $\begingroup$ @MichaelE2 That's a problem, you can use True instead or protect the symbol tt. $\endgroup$
    – wul
    Commented Aug 27, 2019 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.