Skip to main content
added 178 characters in body
Source Link
István Zachar
  • 47.2k
  • 20
  • 145
  • 293

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}}

A bit more general version with Drop and ReplacePart:

list = List /@ Range@8
{from, to} = {3, 6}; (* specify first and last position 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}}

A bit more general version with Drop and ReplacePart:

list = List /@ Range@8
{from, to} = {3, 6}; (* specify first and last positions 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}}
Source Link
István Zachar
  • 47.2k
  • 20
  • 145
  • 293

A bit more general version with Drop and ReplacePart:

list = List /@ Range@8
{from, to} = {3, 6}; (* specify first and last position 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}}