Function Repository Resource:

 RaggedTranspose

Source Notebook

Transpose a list of lists that have different lengths

Contributed by: Eric James Parfitt

ResourceFunction["RaggedTranspose"][{list1,list2,…}]

gives a list whose ith element is the list of ith elements of those listj that have at least i elements, in the order in which the listj appear.

Details

ResourceFunction["RaggedTranspose"][{list1,list2,…}] can be thought of as a Transpose of the lists listj aligned on the left, in which missing entries are omitted rather than padded.
The length of the result is the length of the longest listj. The ith sublist of the result has one element for each listj whose length is at least i.
Empty lists among the listj contribute nothing to the result.
When all of the listj have the same length, ResourceFunction["RaggedTranspose"][{list1,list2,…}] gives the same result as Transpose[{list1,list2,…}].
The elements of the listj can be any expressions, including lists.
ResourceFunction["RaggedTranspose"] evaluates only when its argument is a list of lists.

Examples

Basic Examples (2) 

Transpose a list of lists of different lengths:

In[1]:=
ResourceFunction[
 "RaggedTranspose"][{{"a", "b", "c", "d"}, {"e", "f"}, {"g", "h", "i"}}]
Out[1]=

Transpose a triangular array:

In[2]:=
ResourceFunction["RaggedTranspose"][Table[Range[n], {n, 5}]]
Out[2]=

Scope (3) 

Empty lists contribute nothing:

In[3]:=
ResourceFunction["RaggedTranspose"][{{1, 2}, {}, {3, 4, 5}}]
Out[3]=

The elements can themselves be lists:

In[4]:=
ResourceFunction["RaggedTranspose"][{{{1, 2}}, {{3, 4}, {5, 6}}}]
Out[4]=

An empty list gives an empty list:

In[5]:=
ResourceFunction["RaggedTranspose"][{}]
Out[5]=

Applications (3) 

Interleave lists of different lengths, taking one element from each list in turn:

In[6]:=
Catenate[
 ResourceFunction[
  "RaggedTranspose"][{{1, 2, 3, 4, 5}, {"a", "b"}, {"x", "y", "z"}}]]
Out[6]=

Total the columns of a table whose rows have different lengths:

In[7]:=
Total /@ ResourceFunction["RaggedTranspose"][{{3, 1, 4, 1}, {5, 9}, {2, 6, 5}}]
Out[7]=

Find the conjugate of an integer partition by transposing its Ferrers diagram:

In[8]:=
Length /@ ResourceFunction["RaggedTranspose"][
  ConstantArray["x", #] & /@ {5, 3, 3, 1}]
Out[8]=

Properties and Relations (3) 

For rectangular arrays, RaggedTranspose gives the same result as Transpose:

In[9]:=
ResourceFunction["RaggedTranspose"][{{1, 2, 3}, {4, 5, 6}}]
Out[9]=
In[10]:=
Transpose[{{1, 2, 3}, {4, 5, 6}}]
Out[10]=

For any input other than an empty list, RaggedTranspose gives the same result as Flatten with the level specification {{2},{1}}:

In[11]:=
ResourceFunction["RaggedTranspose"][{{a, b, c, d}, {e, f}, {g, h, i}}]
Out[11]=
In[12]:=
Flatten[{{a, b, c, d}, {e, f}, {g, h, i}}, {{2}, {1}}]
Out[12]=

When the lengths of the lists do not increase, applying RaggedTranspose twice gives back the original list:

In[13]:=
ResourceFunction["RaggedTranspose"][
 ResourceFunction["RaggedTranspose"][{{a, b, c}, {d, e}, {f}}]]
Out[13]=

Possible Issues (1) 

When a list is longer than one before it, applying RaggedTranspose twice does not give back the original list:

In[14]:=
ResourceFunction["RaggedTranspose"][
 ResourceFunction["RaggedTranspose"][{{a, b}, {c, d, e}}]]
Out[14]=

Neat Examples (1) 

Color the entries of a ragged array by row and show the array next to its ragged transpose:

In[15]:=
rows = MapThread[
   Table[Style[Subscript[x, #1, j], ColorData[97][#1]], {j, #2}] &, {Range[5], {6, 3, 5, 2, 4}}];
Row[{Grid[rows, Frame -> All], Grid[ResourceFunction["RaggedTranspose"][rows], Frame -> All]}, Spacer[30]]
Out[15]=

Publisher

Eric James Parfitt

Version History

  • 1.0.0 – 05 October 2026

Related Resources

Author Notes

In the future, this could be made to work with arrays of depth more than 2, or to allow padding for missing entries in transposed ragged arrays.

License Information