16
$\begingroup$

This seems like it should be a simple question, but I am running into some difficulty in doing this with Mathematica. Right now, I have a list like this:

data1={0, 0, 0, 0, 0, 0, 3, 1, 10, 3, 11, 1, 0, 0, 32, 0, 1, 0, 5, 0, 2, 0, 25, 0, 1, 0, 1, 
       0, 0, 0, 0, 7, 0, 0, 0, 0, 13, 4, 0, 5, 0, 0, 2, 3, 4, 0, 0, 95, 4, 16, 11, 2, 0, 0, 
       81, 35, 0, 0, 0, 33, 0, 0, 0, 0, 0, 5, 42, 0, 0, 0};

I want to insert "1997" into the list after each element and transpose it, so that it will look like so: {{1997,0},{1997,0},{1997,0}...}. So far so good.

Unfortunately, the only way I know how to do this is to manually create a list of equal length to data1 (70 "1997"s in a row). I also do not know how to create a list that is just 70 "1997"s in a row. I've plumbed the documentation and tried every command I can think of, but the closest I can get are either functions or a list that resembles {{1997,0,1997,0,1997,0...} etc.

$\endgroup$
7
  • 5
    $\begingroup$ Ian, you have great talent for asking questions that have a thousand and one ways to do them. $\endgroup$
    – rcollyer
    Apr 25, 2012 at 22:32
  • 2
    $\begingroup$ There are several good solutions to your problem below, but as an aside about your question of how to generate a list, there are a couple of easy ways: ConstantArray[1997,70] or Table[1997,{70}]. $\endgroup$
    – wxffles
    Apr 25, 2012 at 22:49
  • 1
    $\begingroup$ @wxffles Thanks for this. It's too bad ConstantArray isn't in the "list manipulations" docs section, as it's a great command to have in one's list toolkit! $\endgroup$ Apr 25, 2012 at 22:52
  • 4
    $\begingroup$ It seems I've found a pretty fast and simple method based on Tuples. This question is really good example for testing efficiency of various methods. $\endgroup$
    – Artes
    Apr 26, 2012 at 2:10
  • 2
    $\begingroup$ Ian, I find ConstantArray to verbose, for my tastes, though, I do use it. If I want something terse, I use Array[1997&, 70]. I doubt it is faster to execute, but it is a lot faster to type! However, Developer`PackedArrayQ[Array[...]] == False, while it is True for ConstantArray, implying a speed hit for Array. $\endgroup$
    – rcollyer
    Apr 26, 2012 at 2:49

5 Answers 5

26
$\begingroup$

Another possibility:

Thread[{1997,data1}]
$\endgroup$
0
32
$\begingroup$

Absolutely unbeatable :

Tuples[{{1997}, data1}]

All other methods are much slower even Verbeia's {1997,#}&/@ data1 or dws' Thread[{1997, data1}]

Tuples[{{1997}, data1}] === ({1997, #} & /@ data1) ===  Thread[{1997, data1}]
True

We compare the most efficient methods for a long list :

 list = RandomChoice[data1, 10^7];

and results :

Thread[{1997, list}]; // AbsoluteTiming // First
{1997, #} & /@ list; // AbsoluteTiming // First
Tuples[{{1997}, list}]; // AbsoluteTiming // First
1.6000000
0.8470000
0.1960000

Other methods just for completeness:

Inner[List, Table[1997, {Length @ data1}], data1, List]

Outer[List, {1997}, data1] // Flatten[#, 1] &

Transpose@{Table[1997, {Length @ data1}], data1}
$\endgroup$
7
  • 2
    $\begingroup$ Apparently, we have a winner in Tuples. I've never used the second form, so I don't think of it. You already had my +1, but I'd give you another for that, if I could. $\endgroup$
    – rcollyer
    Apr 26, 2012 at 2:21
  • 1
    $\begingroup$ @rcollyer Thank You ! I knew there was a simple and fast method, however I couldn't find it and tried many strange ways until Tuples. $\endgroup$
    – Artes
    Apr 26, 2012 at 2:27
  • $\begingroup$ I understand about attempting to use strange ways to solve a problem. The binary tree method, as written, is 4 times slower than any other method. Even with the speed ups! :) $\endgroup$
    – rcollyer
    Apr 26, 2012 at 2:41
  • $\begingroup$ I haven't read yet carefully those "strange ways" but that seems interesting. $\endgroup$
    – Artes
    Apr 26, 2012 at 2:49
  • 2
    $\begingroup$ Nice work! For your "other methods" ConstantArray will be an order of magnitude faster than Table. $\endgroup$
    – Mr.Wizard
    Apr 26, 2012 at 4:57
14
$\begingroup$

The following works and is the way I would recommend:

{1997,#}&/@ data1

There is no need to create a list of 1997s of the same length as your original data.

I can think of a few other ways but they are more roundabout.

This version uses the optional third argument of Riffle.

Partition[Riffle[data1, 1997, {1, 2 Length[data1] - 1, 2}], 2]

There is no point using FoldList if you aren't using the intermediate values of the lists in your calculations, but the following works if what you really want is something more complicated than just joining 1997.

FoldList[{1997, #2} &, {1997, First[data1]}, Rest@data1]
$\endgroup$
0
7
$\begingroup$

As always, there are several ways to skin the cat. Here's yet another one:

data1 /. x_Integer :> {1997, x}

Use x_Real or x_?NumericQ as necessary, if your list is not exactly made of integers as in the example.

$\endgroup$
5
$\begingroup$

One more:

ArrayFlatten@{{1997, List /@ data1}}
$\endgroup$
2
  • $\begingroup$ +1 for an oft-forgotten function. :-) $\endgroup$
    – Mr.Wizard
    Jan 9, 2014 at 9:14
  • $\begingroup$ @Mr.Wizard It's a nice function to get some points. :) tks $\endgroup$
    – Murta
    Jan 9, 2014 at 9:20

Your Answer

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

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