Construct a list from two other lists of unequal length

Given two lists of unequal length:

ind = Range[0, 4];

color = {"red", "green", "blue", "black"};


how can I produce efficiently the following list which is as long as the shorter of the two?

{{"red", 0}, {"green", 1}, {"blue", 2}, {"black", 3}}

• Use Transpose. Commented Mar 5, 2017 at 0:11
• @C.E. Thanks for the comment but the lists do not have the same length. Commented Mar 5, 2017 at 0:14
• ok, I missed that. Then you have to shorten the first list to match the length of the second list. You could for example use Transpose[{color, Take[ind, Length[color]]}. Commented Mar 5, 2017 at 0:24
• @C.E. Thanks again. I was wondering if there is a built-in function that "understands" that the two lists have different lengths and combines them in the proper manner, like zip function in Python. Commented Mar 5, 2017 at 0:30
• To closers: I think this is a legitimate question given that the lists are of unequal length. Commented Mar 5, 2017 at 3:38

Some ideas:

Partition[Riffle[color, ind, {2, -1, 2}], 2]

Flatten[{color, ind}, {2}] // Cases[{_, _}]

ind ~Riffle~ color ~Partition~ 2 ~Reverse~ 2

MapIndexed[{#, Extract[ind, #2]} &, color]

Take[#, All, Min[Length /@ #]]\[Transpose] &[{color, ind}]


All produce:

{{"red", 0}, {"green", 1}, {"blue", 2}, {"black", 3}}


I'll note that the last method, which was perhaps my most serious attempt to answer this pragmatically, can be applied to any number of lists:

fn = Take[#, All, Min[Length /@ #]]\[Transpose] &;

fn[{{1, 2, 3}, Alphabet[], 2^Range@5}]

{{1, "a", 2}, {2, "b", 4}, {3, "c", 8}}

• You have once again lived up to your username. Commented Mar 5, 2017 at 20:59

Here is a function to do it for any two lists. It doesn't care about the order in which the lists appear as arguments.

makePairs[a_List, b_List] :=
Transpose[Take[#, Min[{Length @ a, Length @ b}]] & /@ {a, b}]


makePairs[color, ind]


{{"red", 0}, {"green", 1}, {"blue", 2}, {"black", 3}}

makePairs[ind, color]


{{"red", 0}, {"green", 1}, {"blue", 2}, {"black", 3}}

• FWIW that's pretty similar to my last method: Take[#, All, Min[Length /@ #]]\[Transpose] & Commented Mar 5, 2017 at 6:23
• @Mr.Wizard. The methods might be similar, but the programming philosophy illustrated by the two answer differ enough that I think this deserves to stand. Commented Mar 5, 2017 at 6:25
• You mean you're not a fan of the shotgun answer? :^) Commented Mar 5, 2017 at 6:27
• @Mr.Wizard. I like shotgun answers. I also think there should be room for other kinds. Commented Mar 5, 2017 at 6:35
• No argument from me on that point! Commented Mar 5, 2017 at 6:55

Another variant:

DeleteCases[
Missing, ___}]

Block[{i = 1}, {#, ind[[i++]]} & /@ color]


{{"red", 0}, {"green", 1}, {"blue", 2}, {"black", 3}}

n = Range[0, 4];

c = {"red", "green", "blue", "black"};


Using TransposeTableau by Ed Pegg Jr and Steven Skiena

TransposeTableau = ResourceFunction["TransposeTableau"];

Cases[{_, _}] @ TransposeTableau[{c, n}]


{{"red", 0}, {"green", 1}, {"blue", 2}, {"black", 3}}

Cases[{_, _}] @ TransposeTableau[{{1, 2}, c}


{{1, "red"}, {2, "green"}}

ind = Range[0, 4]; color = {"red", "green", "blue", "black"};

GeneralizedMapThread = ResourceFunction["GeneralizedMapThread"];

{{"red", 0}, {"green", 1}, {"blue", 2}, {"black", 3}}