# Using Tuples for creating the sample space [closed]

I want to use Tuples (if you know better way please let me know) to create the sample space for some events. Assume that I have 2 events each of which can change to a known number (in the case of a coin it is 2). I want to get all of the possible combinations. Something like this:

{0 0} {0 1} {1 0} {1 1}


I can use nested for loop but prefer to not.

I used Tuples and Do (see the code below), but it does not give the right answer. What is the problem?

sampleSpaces =

TableForm[Tuples[{i, j}, 2]]], {i, 0, 1}, {j, 0, 1}];

• Table is what you wanted here. TableForm is a formatting wrapper, and should not be used in these cases, only to do visual formatting. Commented Jan 21, 2022 at 22:16
• To add to the comment by @CATrevillian there are extra brackets in the code you provided. Can you try to run sampleSpaces = Table[Tuples[{i, j, k}, 3], {i, 0, 2}, {j, 0, 2}, {k, 0, 2}] and let us know if it works?
– user49048
Commented Jan 21, 2022 at 22:21
• @Parviz well perhaps you could consider giving a minimal working example (MWE) with the expected outcome so the rest of us can try to help? In the way that the question is written, fixing the code you provided seems the only alternative.
– user49048
Commented Jan 21, 2022 at 22:39
• Tuples[{0, 1}, 2]? Commented Jan 21, 2022 at 23:07
• RandomChoice[Tuples[{0, 1}, 2], 10]? Commented Jan 21, 2022 at 23:08

Using the correct syntax for the command you give we have:

sampleSpaces = Table[Tuples[{i, j}, 2], {i, 0, 1}, {j, 0, 1}];


Then we re-arrange the list in the following manner:

ArrayReshape[sampleSpaces, {16, 2}]


and finally we delete duplicates as

DeleteDuplicates[ArrayReshape[sampleSpaces, {16, 2}]]


which yields

{{0, 0}, {0, 1}, {1, 0}, {1, 1}}


and can be generalized, I think, in a straightforward manner.

Edit: you might find it useful to compare sampleSpaces // Dimensions to the number that you see in the ArrayReshape command.

• Thank you for your help. There is hidden bug in the code. Tuples create all possible combinations include repeating one of the element, in above code if you change the range of i to 0 to 2 it will produce wrong answer. Commented Jan 22, 2022 at 0:47
• @Parviz you need to make it more clear what it is that you want. At this stage I do not see why you do not just use something like Tuples[{0,2},2]. Please, update your question with this additional essential information. Commented Jan 22, 2022 at 1:27
• As @CATrevillian mentioned Tuples[{0,2},2] is the most minimal and elegant way to approach. I only included the above as an answer, because it was too long for a comment. Also, when I mentioned that you could give a MWE before, I just meant that should explain in the OP with the original numbers you gave to give the expected result, so we can match it in mathematica. And as I mentioned in my comment, I think that Tuples[{0, 1}, 3] gives you what you are after -if I recall the first version of the O.P.
– user49048
Commented Jan 22, 2022 at 1:52
• Thank you for your help. Problem with above solution is that, when I change the range of i {i,0,2}, I will have answer like {2 2 2}, which is not correct (j and k can not get 2 as the value). Commented Jan 24, 2022 at 18:33
• Solution: Tuples[{Range[0, 3], Range[0, 4], Range[0, 8]}] Commented Feb 4, 2022 at 19:34