transpose
Turns a collection of rows into a collection of columns.
Lecture
transpose is zip generalized to any
number of iterables: the n-th output list holds the n-th element of
every input row that still has one. Where zip/zip3
take each row as a separate argument (because Dart can't express
variadic generics), transpose takes all the rows as a
single iterable of iterables —
transpose([row1, row2, row3]) — so it works for any row
count decided at runtime.
Unlike zip, which stops as soon as the shortest input runs
dry, transpose keeps going as long as any row still
has values left — a shorter row just stops contributing to later output
lists, which may end up shorter than the row count. It only stops
entirely once every row is exhausted.
Demo 1 · Basics & ragged rows
Demo 2 · Async
Try it yourself
Exercise: transpose the matrix (rows become columns).