Tuples

Tuples[list,n]

generates a list of all possible ntuples of elements from list.

Tuples[{list1,list2,}]

generates a list of all possible tuples whose i^(th) element is from listi.

Tuples[list,{n1,n2,}]

generates a list of all possible n_(1)n_(2) arrays of elements in list.

Details

  • The elements of list are treated as distinct, so that Tuples[list,n] for a list of length k gives output of length kn.
  • Tuples[list,n] is equivalent to Tuples[Table[list,n]].
  • The order of elements in Tuples[list,n] is based on the order of elements in list, so that Tuples[{a1,,ak},n] gives {{a_(1),a_(1),...,a_(1)},{a_(1),a_(1),...,a_(2)},...,{a_(k),a_(k),...,a_(k)}}.
  • The object list need not have head List. The head at each level in the arrays generated by Tuples will be the same as the head of list.

Examples

open allclose all

Basic Examples  (3)

All possible 3-tuples of 0 and 1:

The first element given is always picked first:

All ways of picking one element from each list:

Generalizations & Extensions  (3)

Each occurrence of an element is picked separately:

All possible 2×2 arrays formed from a and b:

Tuples with any head:

Applications  (8)

Digits of successive base-2 numbers:

All possible trigrams of A and B:

Neighborhood rules for an elementary cellular automaton:

Digit sequences, or evolution of a substitution system:

2D lattice of points:

Connect pairs of points in all possible ways:

Properties & Relations  (4)

Tuples[list,n] is equivalent to Tuples[Table[list,n]]:

All possible digit sequences:

Results from Outer involve extra sublists:

Subsets picks each element at most once, and treats reorderings as equivalent:

Possible Issues  (1)

With one argument, Tuples picks an element from each sublist:

With two arguments, Tuples treats each sublist as a complete element:

Neat Examples  (6)

3D lattice of points:

Wolfram Research (2004), Tuples, Wolfram Language function, https://reference.wolfram.com/language/ref/Tuples.html.

Text

Wolfram Research (2004), Tuples, Wolfram Language function, https://reference.wolfram.com/language/ref/Tuples.html.

CMS

Wolfram Language. 2004. "Tuples." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Tuples.html.

APA

Wolfram Language. (2004). Tuples. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Tuples.html

BibTeX

@misc{reference.wolfram_2023_tuples, author="Wolfram Research", title="{Tuples}", year="2004", howpublished="\url{https://reference.wolfram.com/language/ref/Tuples.html}", note=[Accessed: 19-March-2024 ]}

BibLaTeX

@online{reference.wolfram_2023_tuples, organization={Wolfram Research}, title={Tuples}, year={2004}, url={https://reference.wolfram.com/language/ref/Tuples.html}, note=[Accessed: 19-March-2024 ]}