Remove elements from a list for getting new list ?

Hi,

How do I remove elements from my list for generating a new list like below:

list = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, w};

listNEW = {a, e, i, m, q, w}

Hi Alex,

If the elements to be removed are position based rather than value based then

list[[1 ;; -1 ;; 4]]
(* {a, e, i, m, q, w} *)

If value based then, unless there is some way to programmatically select the values, the list has to be constructed by hand

Complement[list, {b, c, d, f, g, h, j, k, l, n, o, p, r, s, t}]
(* {a, e, i, m, q, w} *)

Also look at DeleteCases.

Hi Joel,

Thanks for pointing out DeleteCases. Important if order needs to be preserved. Similar to Union and DeleteDuplicates.

DeleteCases[list, Alternatives @@ {b, c, d, f, g, h, j, k, l, n, o, p, r, s, t}]

or

DeleteCases[list, x_ /; MemberQ[{b, c, d, f, g, h, j, k, l, n, o, p, r, s, t}, x]]

will preserve the order but Complement will not, the result is sorted.

Complement[Reverse@list, {b, c, d, f, g, h, j, k, l, n, o, p, r, s, t}]
(* {a, e, i, m, q, w} *)

DeleteCases[Reverse@list, x_ /; MemberQ[{b, c, d, f, g, h, j, k, l, n, o, p, r, s, t}, x]]
(* {w, q, m, i, e, a} *)