# Combining heads of lists so that you can create a nested list from two sublists

I have a list such as list = {1,4,3,4,6,3,1,2,3,4 ... } and a range, say, range = Range.

I want to obtain the following output:

{{0,1}, {1,4}, {2,3}, {3,4}, {4,6}, ... {nth element of list, nth element of range}}


where n is basically Range[Length[list]].

The general question here would be how can I create a list from the $a_i$th index of list a, the $b_k$th index of list b and so on.

I would have tried MapThread but I'm trying to use the Histogram3D function so I needed this "nice" list.

• Hi, I provided an answer to your question, I'm not quite sure what you mean with the a_i --> b_k list generation. If you have a specific problem with data visualization (e.g. Histogram3D) then you can just ask that question as well.
– Timo
Apr 7, 2012 at 16:56

You can do

Transpose@{ Range@20000 - 1, yourList }


Or more generally if yourList is of varying length

Transpose@{ Range@First@Dimensions@yourList - 1, yourList }


Extracting a part of the list, say from index i to index j can be done with Partor Take

newList = Transpose@{ yourList, Range@First@Dimensions@yourList };
newList[[i;;j]]
(* or *)
Take[newList,{i,j}]

• oh. brilliant. thank you ! didn't see that Apr 7, 2012 at 16:49
• eh.. also! what does '@' do exactly? I see @@ and @@@ under apply, but no @. Edit: found it under 'map' Apr 7, 2012 at 16:52
• Thank you! That helps. I was wondering what to do about varying length. Do you need to do Range@First@Demensions@yourList -1 though? because doesn't Range give a list which is the same dimension + 1? data = {1,2} Ex. Range[0,Length[data]] --> {0,1,2} if I was to have 0 be the first number that is. Apr 7, 2012 at 17:03
• I missed the starting with 0 bit. You are correct about just substracting one from Range@... The @ notation is equivalent to f[g[a]] === f@g@a. It's just a convenient shorthand to get rid of all those brackets.
– Timo
Apr 7, 2012 at 19:13
• @ is not Map, /@ is. f@x just means f[x] whereas f/@ {x,y,z} does the same as Map[f,{x,y,z}], which yields {f[x],f[y],f[z]}. Apr 7, 2012 at 22:22

I believe that MapIndexed is the most canonical method:

list = {1, 4, 3, 4, 6, 3, 1, 2, 3, 4};

MapIndexed[{#2[] - 1, #} &, list]

{{0, 1}, {1, 4}, {2, 3}, {3, 4}, {4, 6}, {5, 3}, {6, 1}, {7, 2}, {8, 3}, {9, 4}}


However, Transpose and Range is faster when list is a packed array.

• pardon me but ... canonical? What do you mean in that sense? Apr 8, 2012 at 20:26
• @Eiyrioü pardon me if I am abusing terminology. I mean that MapIndexed is the typical, expected, or intended way to accomplish this. It is the built-in function that does exactly this. Apr 8, 2012 at 20:28
• Interesting I did not know that. Thanks :D. About the terminology I have no clue whether you are or not, I was just wondering if there was some notation/convention I did not know about. Thanks! Apr 8, 2012 at 20:41

If you've just started learning Mathematica, also check out the Table function, which has many applications:

Table[{i - 1, list[[i]]}, {i, 1, 20}]

   {{0, 1}, {1, 4}, {2, 3}, {3, 4}, {4, 6}, ... }

• This way is slower for the big list. Nov 2, 2020 at 3:22