# Delete elements from a list

I have two lists, for example

s={{1,3,2},{1,3,2},{2,3,1},{3,1},{2,3,1}};
c={{3},{1,2},{3}};


First element of c means that number 1 from s[[3]] must be deleted. Number is a position of this element in c.

Second element of c: delete number 2 from s[[{1,2}]].

Third: delete number 3 from s[[3]]

The result must be

(* {{1,3},{1,3},{2},{3,1},{2,3,1}} *)


Another example

s={{2,1,3},{2,3,1},{1,2,3},{2,3}};
c={{1},{2,4},{3}};
(* {{2,3},{3,1},{1,2},{3}} *)


One more question. Also two lists:

s={{{1,3},{1,2},{3,2},{1},{3},{2}},
{{1,3},{1,2},{3,2},{1},{3},{2}},
{{1},{2},{3}},
{{3},{2},{1}}};
c={{3},{1,2},{}};


So, from s[[3]] (* {{1},{2},{3}} *) it's necessary to delete ALL elements which contain number 1: {1} (* {{2},{3}} *)

From s[[{1,2}}]] - all elements which contain number 2

(* {{{1,3},{1},{3}},
{{1,3},{1},{3}},
{{2},{3}},
{{3},{2},{1}}} *)


How can I do it?

You may use MapIndexed, ReplaceAll, and Nothing.

## Delete Numbers

s1 = {{1, 3, 2}, {1, 3, 2}, {2, 3, 1}, {3, 1}, {2, 3, 1}};
c1 = {{3}, {1, 2}, {3}};

MapIndexed[(s1[[#1]] = (s1[[#1]] /. {First@#2 -> Nothing})) &, c1];
s1

(* {{1, 3}, {1, 3}, {2}, {3, 1}, {2, 3, 1}} *)


## Delete Sublists

s2 = {
{{1, 3}, {1, 2}, {3, 2}, {1}, {3}, {2}},
{{1, 3}, {1, 2}, {3, 2}, {1}, {3}, {2}},
{{1}, {2}, {3}},
{{3}, {2}, {1}}
};
c2 = {{3}, {1, 2}, {}};

MapIndexed[(s2[[#1]] = (s2[[#1]] /. {{___, First@#2, ___} -> Nothing})) &, c2];
s2

(*
{{1,3},{1},{3}}
{{1,3},{1},{3}}
{{2},{3}}
{{3},{2},{1}}
*)


Hope this helps.

• if possible can you tell more about the aim of author who asked the question I have so much tried to understand your solution but I think I cannot understand the main question! Commented Jul 25, 2016 at 13:29
• @Irreversible OP has a list of positions c and wants to perform some action on the list s that is a function of each position and the position's index in list of positions. MapIndexed is mapping its function over c. MappedIndexed's function takes two arguments. The first is the item from the list it is mapping over (#1) and the second is the index of that item in the list (#2). #1 is obtained from the list. While #2 is generated by the system for you; you don't need to provide it. Try MapIndexed[{#1,#2}&, Range[6,10]]. Commented Jul 25, 2016 at 21:02
• Thank you so much. OP has used of phrases such as : Number 1 Number 2!!! they are not defined in list-manipulation category. I wish a professional OP edits his/her question. Commented Jul 26, 2016 at 5:32

Do and DeleteCases can do what you want

The first one:

delete[s_, c_] := Module[{s0 = s},
Do[s0[[c[[i]]]] = DeleteCases[s0[[c[[i]]]], i, 2], {i, Length@c}];
s0
]


The second one

delete2[s_, c_] := Module[{s0 = s},
Do[s0[[c[[i]]]] = DeleteCases[s0[[c[[i]]]], {___, i, ___}, 2], {i,
Length@c}];
s0
]