# Access list position from iterator?

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*)
,{i,list}]


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?

• 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. – Michael E2 Jul 16 '17 at 2:46
• Closely related: (84747). Tangentially similar: (19022) – Mr.Wizard Jul 17 '17 at 19:53

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 *)
&,list]


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

• 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. – Mr.Wizard Jul 16 '17 at 10:38
• @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. – Kagaratsch Jul 16 '17 at 13:07
• (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? – Mr.Wizard Jul 16 '17 at 20:00
• @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. – Kagaratsch Jul 17 '17 at 13:16
• 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. – Mr.Wizard Jul 17 '17 at 19:41