0

I'd like to produce a list of $n-$tuples made from the elements of Range[n] coupled with a constant value $m$.

Here's an example, given $n=3$:

Range[3] = {1,2,3}

What I want to obtain is:

{{1,m},{2,m},{3,m}}

I've tried using Tuple, but I couldn't get it to make what I want.

Rodrigo
  • 233
  • 1
  • 4

3 Answers3

3

You can in fact use Tuples[]:

Tuples[{{1, 2, 3}, {"m"}}]
   {{1, "m"}, {2, "m"}, {3, "m"}}
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
3

Benchmark in Mathematica 10.1 of the posted methods, plus one using ArrayFlatten:

tuples[n_] := Tuples[{Range@n, {m}}];
thread[n_] := Thread[{Range@n, m}];
array[n_] := Array[{#, m} &, n];
transpose[n_] := Transpose@{Range@n, ConstantArray[m, n]};
arrayflatten[n_] := ArrayFlatten[{{Range@n ~Partition~ 1, m}}];

m = 17;

Needs["GeneralUtilities`"]
BenchmarkPlot[{tuples, thread, array, transpose, arrayflatten}, Identity]

enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
1

Something like this?

Block[{n = 3}, Array[{#, m} &, n]]
Transpose@{Range[3], ConstantArray[m, 3]} == %
Thread@{Range[3], m} == %%

{{1, m}, {2, m}, {3, m}}

True

True

NonDairyNeutrino
  • 7,810
  • 1
  • 14
  • 29