# Add new coordinate to list of lists with different lengths

I want to turn the list

list1 = {{a},{b,c},{d,e,f,g}}


into

{ {{1,a}}, {{2,b},{2,c}}, {{3,d}, {3,e}, {3,f}, {3,g}} }


How can I do this? I've tried different forms of Map but none is working.

• Maybe MapThread[Thread[{#2, #1}] &, {list1, Range[Length[list1]]}] or Thread[{#[], #[]}] & /@ Transpose[{Range[Length[list1]], list1} ? – b.gates.you.know.what Nov 21 '18 at 16:38
• Perhaps Table[{ConstantArray[i, Length[list1[[i]]]], list1[[i]]}[Transpose], {i, 1, Length@list1}] – user11946 Nov 21 '18 at 16:51
• Thanks, it's working! – AJHC Nov 21 '18 at 16:56
• @b.gatessucks There is a bracket missing right at the end of your second solution. – Titus Nov 21 '18 at 17:05
• Sorry - cut and paste didn't quite work for me in StackExchange - try this - Table[Transpose[{ConstantArray[i, Length[list1[[i]]]], list1[[i]]}], {i, 1, Length@list1}] – user11946 Nov 21 '18 at 18:25

Solutions by @b.gatessucks

MapThread[Thread[{#2, #1}] &, {list1, Range[Length[list1]]}]
(*or*)

Thread[{#[], #[]}] & /@ Transpose[{Range[Length[list1]], list1}]


I could not get @user11946's to work.

My own contribution is a generalisation.

The coordinates follow a geometric sequence: the first element is 1, the next two are 2, the next four are 4 etc. This can be tabulated as

list1 = {{a}, {b, c}, {d, e, f, g}}

bill = Table[
ConstantArray[i,
FoldList[Times, 1, Table[2, Length[list1]]][[i]]], {i, 1,
Length[list1]}]


In this thread I found many useful ways for element-wise Join in matrixes. I use one of them and define

threadJoin = Quiet[Re@##] /. Re -> List &;


MapIndexed[Thread[{#2[], #}] &, list1]