# Need list of unequal lengths to be paired with year

Suppose I have a list of five lists, each with a different number of elements

   list = {RandomReal[{0, 1}, 7], RandomReal[{0, 1}, 9],
RandomReal[{0, 1}, 5], RandomReal[{0, 1}, 11],
RandomReal[{0, 1}, 13]}


Each of these lists occur in certain unique years (the number of years matching the number of lists)

yrs = Range[1990, 1994]


I can laboriously make a set of pairs for each year

 Transpose[{ConstantArray[1990, Length[list[[1]]]], list[[1]]}]
Transpose[{ConstantArray[1991, Length[list[[2]]]], list[[2]]}]


But since my actual data is much larger, it would be easier if it were all done in a single blow.

Thanks

## 5 Answers

You can use MapThread with Thread.

With list and yrs in OP then

res = MapThread[Thread[{##}] &, {yrs, list}];


Then for first year

res[[1]]

{{1990, 0.151543}, {1990, 0.944944}, {1990, 0.538527}, {1990, 0.63919}
, {1990, 0.971325}, {1990, 0.636851}, {1990, 0.543734}}


Hope this helps.

• Thanks, I tried every possible combination of Map and MapThread and Thread EXCEPT using two of them together
– Rogo
Commented Apr 16, 2022 at 23:18
f1 = Map[Thread] @* Thread;

f1 @ {yrs, list}


Additional variations:

f2 = MapThread[Thread @* List];

f3 = Map[Thread] @* Thread @* List;

f1 @ {yrs, list} == f2 @ {yrs, list} == f3[yrs, list]

True

SeedRandom[42];
list = {RandomReal[{0, 1}, 7], RandomReal[{0, 1}, 9],
RandomReal[{0, 1}, 5], RandomReal[{0, 1}, 11],
RandomReal[{0, 1}, 13]}
yrs = Range[1990, 1994]

t1 = Inner[Thread[{##}] &, yrs, list, List]


a variation of the answer by @kglr (without compositions):

t2 = Thread /@ Thread[{yrs, list}]


t3 = Thread /@ Transpose[{yrs, list}]


result:

> {{{1990, 0.425905}, {1990, 0.391023}, {1990, 0.347069}, {1990,
>    0.453741}, {1990, 0.555963}, {1990, 0.289169}, {1990,
>    0.296848}}, {{1991, 0.206408}, {1991, 0.32517}, {1991,
>    0.973325}, {1991, 0.258796}, {1991, 0.550582}, {1991,
>    0.717287}, {1991, 0.754353}, {1991, 0.860349}, {1991,
>    0.996966}}, {{1992, 0.739226}, {1992, 0.0383646}, {1992,
>    0.376127}, {1992, 0.296199}, {1992, 0.528142}}, {{1993,
>    0.607352}, {1993, 0.902795}, {1993, 0.840683}, {1993,
>    0.885752}, {1993, 0.527083}, {1993, 0.560605}, {1993,
>    0.924421}, {1993, 0.00157704}, {1993, 0.90062}, {1993,
>    0.367489}, {1993, 0.274115}}, {{1994, 0.97712}, {1994,
>    0.96073}, {1994, 0.478762}, {1994, 0.848851}, {1994,
>    0.352932}, {1994, 0.799515}, {1994, 0.365826}, {1994,
>    0.739896}, {1994, 0.789536}, {1994, 0.807022}, {1994,
>    0.534674}, {1994, 0.164278}, {1994, 0.362973}}}


Well, there is the obvious generalization

f[m_] :=
Transpose[{ConstantArray[yrs[[m]], Length[list[[m]]]], list[[m]]}]


Compare to what you did manually

LinearAlgebraPrivateZeroArrayQ[
f[1] - Transpose[{ConstantArray[1990, Length[list[[1]]]],
list[[1]]}]]

LinearAlgebraPrivateZeroArrayQ[
f[2] - Transpose[{ConstantArray[1991, Length[list[[2]]]],
list[[2]]}]]


both of which give you

which means that the automated and manual results are equivalent.

Create a list with everything together can be done effortlessly

ff = Table[
Transpose[{ConstantArray[yrs[[m]], Length[list[[m]]]],
list[[m]]}], {m, 1, Length@yrs}];


And now

ff[[1]]


gives exactly res[[1]] - see the accepted solution. Equivalently f[1] is also the same with res[[1]]

SeedRandom[0];

a = Range[1990, 1991];
b = {RandomReal[{0, 1}, 2], RandomReal[{0, 1}, 3]};
n = Length[a];


Using Table

Table[Thread[{a[[i]], b[[i]]}], {i, n}]


Using Map

Thread[{a[[#]], b[[#]]}] & /@ Range[n]


Using Function

f = Function[n, Thread[{a[[n]], b[[n]]}], Listable];

f @ Range[n]


All produce

{{{1990, 0.652468}, {1990, 0.63307}}, {{1991, 0.682813}, {1991, 0.566352}, {1991, 0.935202}}}