3
$\begingroup$

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

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.

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – Timo
    Commented Apr 7, 2012 at 16:56

3 Answers 3

10
$\begingroup$

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}]
$\endgroup$
6
  • $\begingroup$ oh. brilliant. thank you ! didn't see that $\endgroup$ Commented Apr 7, 2012 at 16:49
  • $\begingroup$ eh.. also! what does '@' do exactly? I see @@ and @@@ under apply, but no @. Edit: found it under 'map' $\endgroup$ Commented Apr 7, 2012 at 16:52
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Apr 7, 2012 at 17:03
  • $\begingroup$ 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. $\endgroup$
    – Timo
    Commented Apr 7, 2012 at 19:13
  • 1
    $\begingroup$ @ 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]}. $\endgroup$ Commented Apr 7, 2012 at 22:22
3
$\begingroup$

I believe that MapIndexed is the most canonical method:

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

MapIndexed[{#2[[1]] - 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.

$\endgroup$
3
  • $\begingroup$ pardon me but ... canonical? What do you mean in that sense? $\endgroup$ Commented Apr 8, 2012 at 20:26
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Commented Apr 8, 2012 at 20:28
  • $\begingroup$ 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! $\endgroup$ Commented Apr 8, 2012 at 20:41
2
$\begingroup$

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}, ... }
$\endgroup$
1
  • $\begingroup$ This way is slower for the big list. $\endgroup$
    – Nam Nguyen
    Commented Nov 2, 2020 at 3:22

Your Answer

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

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