lzip

MAKR 2009-04-09 a list zipper. This was briefly discussed on the chat today by RS, kruzalex, and teo. The implementation could be like:

    proc lzip {l1 l2} {
        set res {}
        foreach x $l1 y $l2 {
            lappend res $x $y
        } 
        set res
    }

would lead to

    $ lzip {a b c} {1 2 3}
    a 1 b 2 c 3

A more generic form is available as [interleave].


JAL 2021-06-06 If you want to zip more than 2 lists together, here is a more complex implementation.

proc lzip {args} {
    set list_l $args
    set zip_l []
    while {1} {
        set cur    [lmap a_l $list_l { lindex $a_l 0 }]
        set list_l [lmap a_l $list_l { lrange $a_l 1 end }]

        if {[join $cur {}] == {}} {
            break
        }
        lappend zip_l $cur
    }
    return $zip_l
}

For example

  # leaving off B3 just to show the behavior when one list doesn't have as many elements as others.
  lzip {A1 A2 A3} {B1 B2} {C1 C2 C3}
   =>
  {A1 B1 C1} {A2 B2 C2} {A3 {} C3}

And in this code, if one of the lists runs out of elements, but other list(s) still have elements, we insert an empty element, which I think is the behavior that people would expect. Alternatives might be to terminate the zipping.

I think of zipping lists together as a "rotation by 90 degrees", e.g. translating from lists of rows to lists of columns. To elaborate: if you have a list of rows, where each row is a list of elements, but you instead want a list of columns (of elements) then lzip can do that for you simply.