A bit more general version with Drop
and ReplacePart
:
list = List /@ Range@8
{from, to} = {3, 6}; (* specify first and last positionpositions to be joined *)
ReplacePart[Drop[list, {from + 1, to}], from -> Join @@ Take[list, {from, to}]]
{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}}
{{1}, {2}, {3, 4, 5, 6}, {7}, {8}}
With ReplaceAll
and Repeated
:
list /. {x : Repeated[_, {from-1}], y : Repeated[_, {to-from+1}], z___} :> {x, Join@y, z}
{{1}, {2}, {3, 4, 5, 6}, {7}, {8}}