How to take the beginning and end parts of a list with simpler syntax?
Update: You can also try MapAt
:
a = Range[10];
a = MapAt[b &, a, {{;; 3}, {7 ;;}}]
{b, b, b, 4, 5, 6, b, b, b, b}
Or ReplaceAll
a = Range[10];
a /. Alternatives @@ Drop[a, 4;;6] -> b
{b, b, b, 4, 5, 6, b, b, b, b}
Original answer: Try Drop
:
Drop[Range @ 10, {4, 6}]
{1, 2, 3, 7, 8, 9, 10}
Drop[Range @ 10, 4 ;; 6]
{1, 2, 3, 7, 8, 9, 10}
Drop[CharacterRange["a", "j"], {4, 6}]
{"a", "b", "c", "g", "h", "i", "j"}
If it is okay to perform two assignments instead of one, then we can write:
(a[[#]] = b) & /@ {1 ;; 3, 7 ;; 10}
Scan
is probably better since it does not bother constructing the result list that we are just going to discard anyway:
Scan[(a[[#]] = b) &, {1 ;; 3, 7 ;; 10}]
Perhaps this?:
ReplacePart[Range@10, i_ /; Not[4 <= i <= 6] :> b]
(* {b, b, b, 4, 5, 6, b, b, b, b} *)