3
$\begingroup$

What would be a nice way to split the list

list = {1,2,3,{a,b,c},4}

into

{{1,2,3,a,4},{1,2,3,b,4},{1,2,3,c,4}}
$\endgroup$

4 Answers 4

10
$\begingroup$
Thread@list

gives

{{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}
$\endgroup$
1
  • 1
    $\begingroup$ arg! beat me to it! +1 $\endgroup$
    – rcollyer
    Commented Nov 20, 2014 at 18:18
3
$\begingroup$
Distribute[list, List]

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

$\endgroup$
2
$\begingroup$
With[{pos = Position[list, _List][[1, 1]]}, 
 ReplacePart[list, pos -> #] & /@ list[[pos]]]
(*{{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}*)
$\endgroup$
1
  • $\begingroup$ Thanks for this. I accepted a different answer because the way I phrased the question it was technically better, but this answer is closer to what I really needed in practice! $\endgroup$
    – Heterotic
    Commented Nov 21, 2014 at 10:18
1
$\begingroup$
ReplacePart[list, 4 -> #] & /@ list[[4]]
(*{{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}*)
$\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.