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$
10
$\begingroup$
Thread@list

gives

{{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}
$\endgroup$
  • 1
    $\begingroup$ arg! beat me to it! +1 $\endgroup$ – rcollyer Nov 20 '14 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$
  • $\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 Nov 21 '14 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, privacy policy and cookie policy

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