5

I have the following two lists:

{a,b,c,d,e,f} and {x,y,z}

and would like to use Partition to create groups of the form:

{a,x}, {b,y}, {c, z}

My first approach is to use:

list1 = {a,b,c,d,e}
list2 = {x,y,z}
flat = Flatten[{list1, list2}]

The output of which is a single list that I intended to use Parition to create the list of pairs.

So, if I try Partition

Partition[flat, 2, 5}

The result is: {{a, b}, {x, y}}

Not the desired result, but I can't find the right parameters of Partition to generate the correct list of pairs.

I can drop the the final elements of list1 based on the length of list2 and then use Transpose.

Just curious if Partition can get to the same result.

dixontw
  • 441
  • 2
  • 4

1 Answers1

7

I think a solution using solely Partition does not exist, but with Transpose:

list1 = {a, b, c, d, e};
list2 = {x, y, z};
flat = Flatten[{list1, list2}];

Partition[flat, 3, 5]\[Transpose]
{{a, x}, {b, y}, {c, z}}

Or Part:

Partition[flat, 6, 1][[All, {1, -1}]]
{{a, x}, {b, y}, {c, z}}

Perhaps Riffle is of interest too:

list1 ~Riffle~ list2 ~Partition~ 2
{{a, x}, {b, y}, {c, z}, {d, x}}

And Flatten has a ragged form:

Flatten[{list1, list2}, {2}]

% // Cases[{_, _}]
{{a, x}, {b, y}, {c, z}, {d}, {e}}

{{a, x}, {b, y}, {c, z}}

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371