1
$\begingroup$

Suppose we have a list given below

OurList={a,b,c,d,e}

Is there a way to attribute a number to each element of OurList so that we could form a table of points? The expected result is

OutTable={{1,a},{2,b},{3,c},{4,d},{5,e}}
$\endgroup$
2
  • 1
    $\begingroup$ Transpose[{Range[1, Length@ourList], ourList}]. Please do not start variable names with uppercase characters. OR Thread[{Range[1, Length@#], #}] &@ourList $\endgroup$
    – Syed
    Mar 16, 2022 at 12:34
  • $\begingroup$ Look at List Manipulation $\endgroup$
    – Bob Hanlon
    Mar 16, 2022 at 13:26

1 Answer 1

1
$\begingroup$

There are may be 10 others ways to do this, but this is good problem for using MapIndexed

lis = {a, b, c, d, e}
MapIndexed[{First[#2], #1} &, lis]

Mathematica graphics

MapIndexed[{2*First[#2], #1} &, lis]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Does this method generalise to using a formula instead of 1,2,3,4... to label each element of a list in order? For example I might want {{2,a},{4,b}...} instead so the numbering formula is for nth element of a list, the corresponding number should be 2t instead of t, is there a way your method could be extended to include this scenario? $\endgroup$
    – Rescy_
    Mar 16, 2022 at 12:44
  • $\begingroup$ @Rescy_ updated. $\endgroup$
    – Nasser
    Mar 16, 2022 at 12:47

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