Consider a list, e.g.:

list = {a,b,c,d};

One can compose a Table by directly iterating through the list:

tab = Table[
        (*do something with i*)

This is useful, since we iterate through all elements in list without introducing an integer counter and notationally cleaner without having to deal with parts list[[...]]. However, sometimes we need to refer back to the position of i in list. Is there a convenient way to know the position of each i within list without calling Position[list,i]? What I mean is, does the iterator store and provide this information as well, so that there is some way to call it directly?

  • 2
    $\begingroup$ Somehow Table[With[{i = list[[idx]]}, (* do something with i *)], {idx, Length@list}] seems efficient enough, but there is MapIndexed[]. In any case, AFIAK, neither your form of Table nor Map makes the index available. $\endgroup$
    – Michael E2
    Commented Jul 16, 2017 at 2:46
  • $\begingroup$ Closely related: (84747). Tangentially similar: (19022) $\endgroup$
    – Mr.Wizard
    Commented Jul 17, 2017 at 19:53

1 Answer 1


As pointed out by Michael E2 in the comments, a Table function iterator does not make the index available. However, one can use a different function MapIndexed that instead of iterators provides two function arguments #1 and #2 referring to each element and its index respectively as it goes through the list:

tab = MapIndexed[
      (* do something with element `#1` while `#2` stores its position in `list` *)

This is notationally even shorter than the Table. And it generalizes straightforwardly to higher dimensional arrays.

  • $\begingroup$ Based on this self-answer with "exactly what is required" I have marked your question as "already has an answer." Please let me know if you disagree. $\endgroup$
    – Mr.Wizard
    Commented Jul 16, 2017 at 10:38
  • 1
    $\begingroup$ @Mr.Wizard I would say, to find the other question one would already have to know to search for Map, whereas my question is broader as it asks about possible extra hidden information in generic iterators, such as in Table. The MapIndexed solution is more of a workaround solution for that. $\endgroup$
    – Kagaratsch
    Commented Jul 16, 2017 at 13:07
  • $\begingroup$ (1) If your question remains marked as a duplicate it will remain visible (also to search engines) and serve as a redirect to the other question. (2) Nevertheless if you feel that MapIndexed is a workaround rather than exactly what is required I shall reopen this question, but you should change the wording of this self-answer to reflect that. Shall I? $\endgroup$
    – Mr.Wizard
    Commented Jul 16, 2017 at 20:00
  • $\begingroup$ @Mr.Wizard Oh, not really, I mean - I only wrote the comment because it seemed like you wanted to hear my personal opinion. Independent from that, I fully trust you on anything Mathematica! :) If you marked this as duplicate, I guess it is then, regardless of my personal impressions. $\endgroup$
    – Kagaratsch
    Commented Jul 17, 2017 at 13:16
  • 1
    $\begingroup$ Yes, I was and am interested in your personal opinion. As a Community Moderator I'm just a volunteer custodian, trying to do my best for all concerned, not The Hand of God, so I always care how people feel about closes etc. I try to be consistent about my actions which is why I try to explain and/or argue my position, rather than just writing "I have decided" and leaving it at that. $\endgroup$
    – Mr.Wizard
    Commented Jul 17, 2017 at 19:41

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.