cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

Transpose List of Lists without a for loop

I would like to have a method to transpose a list of lists without using a for loop.  That is, some function (call it "Listtranspose" for instance) that flips the "rows" and "columns" of a list of lists, like this:

 

listlist = {{"cat", "dog", "mouse"}, {"meow", "bark", "squeak"}};
tranlist = Listtranspose(listlist);
Show(tranlist);

/*Result:
{{"cat", "meow"}, {"dog", "bark"}, {"mouse", "squeak}}

An alternate implementation could be something like:

 

 

listlist = {{"cat", "dog", "mouse"}, {"meow", "bark", "squeak"}};
nr = NItems(listlist);
col1 = listlist[1::nr][1];
col2 = listlist[1::nr][2];
col3 = listlist[1::nr][3];
translist = EvalList({col1, col2, col3});

I kind of like the latter implementation better as I might only need to transpose just a subset of "columns", not the entire list.

 

 

Of course, this can be done using a For Loop, but the reason for asking for this is that For Loops can be slow.  I work with large blocks of text and I believe this would help speed things up greatly for me.

 

Credit to @vince_faller for asking this originally. Transpose List of Lists without a for loop 

 

 

14 Comments
vince_faller
Super User (Alumni)

Sorry, I was saying  that I was lazy and selfish.  I had an angel emoji but it didn't come through.  Very sorry.  

hogi
Level XII

hogi_0-1725644718314.png

The disadvantage of not having Emojis in the community ; )

hogi
Level XII

Parallel Assign (as 2-dimensional for loop) has no chance to win against the 1D-For Loop
(... or @vince_faller 's approach via data table manipulations)

winner on my system:  DataTable!

hogi_1-1725636479748.png

hogi
Level XII

with just a single vote since 2021, I think the is little chance for 

Listtranspose(listlist);

 

If users found this wish, please also take the time to look at Insert Into: Flatten(0|1) - and vote