My question is, what list p in the following statement returns the list q?


If we think of Tuples as a binary operator then p would be the identity for Tuples.

I thought an empty list would work, but evaluating the following


gives {} instead of {1,2,3} or {{1},{2},{3}} as I had hoped.

The following doesn't work either:


Certainly, I could write a function like the following

altTuples[p_List,q_List]:= If[Length[p]==0,q,Tuples[{p,q}]

That does exactly what I want, but I want to know if I'm missing something. Is there in fact an identity for Tuples? Is there a way to do what I want with Outer? I've tried the obvious solutions with no luck.

  • 1
    $\begingroup$ How do you want your altTuples[] to behave when the length of $p$ is not zero? For example altTuples[{a, b, c}, {d, e, f}] returns Tuples[{a, b, c}, {d, e, f}], probably not what you want. $\endgroup$
    – mjw
    Jul 8, 2019 at 1:19
  • $\begingroup$ I corrected altTuples. Actually I want it to return Tuples[{{a, b, c}, {d, e, f}}]. I think Nothing is what I was looking for. $\endgroup$
    – JAS
    Jul 8, 2019 at 23:27
  • $\begingroup$ What you have above is almost correct (up to a typo). Anyway seems to produce what you want. 'AltTuples[{p,q}]` gives the same output as Tuples[p,q] when the first argument is a list with non-zero length. $\endgroup$
    – mjw
    Jul 8, 2019 at 23:36

4 Answers 4


If {{1},{2},{3}} is fine, you can use Nothing:

Tuples[{Nothing, {1, 2, 3}}]
(* {{1}, {2}, {3}} *)

If you want {1,2,3}, you can Flatten the result, of course.

  • 1
    $\begingroup$ Thank you, this does exactly what I need. $\endgroup$
    – JAS
    Jul 8, 2019 at 23:20

You can use Inactive[Sequence][] as identity like this:




Use TagSetDelayed to define a function that behaves as desired:

iDentity /: {iDentity[___], a : {__}} := iDentity[a]
iDentity /: Tuples[iDentity[a_]] := a

Tuples[{iDentity[], {q}}]


Tuples[{iDentity[blah], {1, 2, 3}}]

{1, 2, 3}

Alternatively, define your function altTuples with two signatures:

altTuples[{tuplesIdentity | {} | Nothing, a_List}] := a
altTuples[x_] := Tuples[x]

altTuples[{{x, y}, {1, 2}}]

{{x, 1}, {x, 2}, {y, 1}, {y, 2}}

altTuples[{tuplesIdentity, {1, 2}}]

{1, 2}

altTuples[{{}, {1, 2}}]

{1, 2}


I think this is what you want:

altTuples[p_List, q_List] := If[Length[p] == 0, q, Tuples[{p, q}]]

The statement

altTuples[{}, {d, e, f}]


{d, e, f}


altTuples[{a, b, c}, {d, e, f}]


{{a, d}, {a, e}, {a, f}, {b, d}, {b, e}, {b, f}, {c, d}, {c, e}, {c, f}}

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.