Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Transpose a list of lists that have different lengths
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. |
Transpose a list of lists of different lengths:
| In[1]:= |
| Out[1]= |
Transpose a triangular array:
| In[2]:= |
| Out[2]= |
Empty lists contribute nothing:
| In[3]:= |
| Out[3]= |
The elements can themselves be lists:
| In[4]:= |
| Out[4]= |
An empty list gives an empty list:
| In[5]:= |
| Out[5]= |
Interleave lists of different lengths, taking one element from each list in turn:
| In[6]:= |
| Out[6]= |
Total the columns of a table whose rows have different lengths:
| In[7]:= |
| Out[7]= |
Find the conjugate of an integer partition by transposing its Ferrers diagram:
| In[8]:= |
| Out[8]= |
For rectangular arrays, RaggedTranspose gives the same result as Transpose:
| In[9]:= |
| Out[9]= |
| In[10]:= |
| 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]:= |
| Out[11]= |
| In[12]:= |
| Out[12]= |
When the lengths of the lists do not increase, applying RaggedTranspose twice gives back the original list:
| In[13]:= |
| Out[13]= |
When a list is longer than one before it, applying RaggedTranspose twice does not give back the original list:
| In[14]:= |
| Out[14]= |
Color the entries of a ragged array by row and show the array next to its ragged transpose:
| In[15]:= |
| Out[15]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License