5
$\begingroup$

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.

$\endgroup$
1
  • 3
    $\begingroup$ Are you aware of this? Flatten[{{a, b, c, d, e, f} , {x, y, z}}, {2}] $\endgroup$
    – Mr.Wizard
    Mar 13, 2020 at 14:21

1 Answer 1

7
$\begingroup$

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}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.