# Seek a quick way to bring standard order back to original after Complement [duplicate]

the Complement gives result in standard order.

But that's not what I want. I want it to keep the original order.

say I have a list

list = {8, 3, 6, 4, 2, 5, 7};


and

compli = Complement[list, {3, 6, 2}]


gives

{4, 5, 7, 8}


But I want

{8, 4, 5, 7}


I write the following code to bring it back to the original order

Reap[If[MemberQ[compli, #], Sow[#]] & /@ list][[2, 1]]


I don't know whether it is good or not. Maybe there is more elegant way to do this.

Finally, I must say I just don't understand why Complement is designed to give standard order. If I want standard order, I can simply use Sort. And now things got more complicated than it should be.

## marked as duplicate by Pinguin Dirk, Mike Honeychurch, Simon Woods, m_goldberg, Leonid ShifrinNov 17 '13 at 13:45

• @PinguinDirk, great, now you are telling us, after all this hard work :) – Nasser Nov 17 '13 at 11:52
• @Nasser: sorry, I am on weekend-mode :) And actually, not sure if it's an exact dupe - but close enough I'd say – Pinguin Dirk Nov 17 '13 at 11:58
• @PinguinDirk OK, I admit my question is a duplicate. Why I can't find the duplicate before I post the question? Have you seen that question before or you just searched and find duplicates? – matheorem Nov 17 '13 at 13:35
• @matheorem I guess this is one of the points of having a coherent community. Someone would remember that the question was asked before (often because that person either asked or answered that question), and point it out. It is not always possible to tell in advance (or even after serious search effort) whether the question has been asked before, even for the most active users. This is just a fact of life. – Leonid Shifrin Nov 17 '13 at 17:31

Cases[list, x_ /; FreeQ[{3, 6, 2}, x]]


{8, 4, 5, 7}

List in Mathematica is like a mathematical set. Hence no order is implied. But you can use SortBy

list = {8, 3, 6, 4, 2, 5, 7};
compli = Complement[list, {3, 6, 2}]
(*{4, 5, 7, 8}*)
SortBy[compli, (First@Position[list, #]) &]

(* {8, 4, 5, 7} *)


Another example: (just to verify)

list = {8, 3, 5, 6, 4, 2, 5, 7, 7, 5, 5};
compli = Complement[list, {3, 6, 2}]
(*  {4, 5, 7, 8} *)
SortBy[compli, (First@Position[list, #]) &]
{8, 5, 4, 7}

• I'd say list isn't like set. Thats why {1, 2} != {2, 1}. – Kuba Nov 17 '13 at 11:20
• @Kuba Only if set is not constrained to be ordered. – Rorschach Nov 17 '13 at 11:26
• @Kuba, but you need to use the correct comparing operator then for comparing sets :) You are using an operator = whose semantics are not defined for use with true Mathematical sets. That is why I said "like" a mathematical set. But for this for another place to discuss I suppose. – Nasser Nov 17 '13 at 11:27
• Yes, you are right. :) @Blackbird I was taught that ordered set is not a set but series. But I admit that is a material for a long talk, here offtopic :) – Kuba Nov 17 '13 at 11:34
• @Kuba, it is a list (for ordered, not series). This is actually an important topic, since some CAS systems support both a set and a list as basic data types. Here is Maple page on this for example: maplesoft.com/support/help/MapleSim/view.aspx?path=set , so this sort of thing can be important in some applications...I remember this was talked about before at mathgroup. – Nasser Nov 17 '13 at 11:41

I would do:

Fold[DeleteCases, list, {3, 6, 2}]

{8, 4, 5, 7}

• Not sure that Fold has any advantages over DeleteCases[list, Alternatives @@ {3, 6, 2}] – Mike Honeychurch Nov 17 '13 at 11:29
• @MikeHoneychurch me too, I just like Fold :) so if it does not matter.. :) – Kuba Nov 17 '13 at 11:30
• @MikeHoneychurch why do you delete your answer? I was just asking you what do you mean by "direct way" – matheorem Nov 17 '13 at 11:49
• @matheorem I deleted because it was a duplicate – Mike Honeychurch Nov 17 '13 at 20:29
Select[list, FreeQ[{3, 6, 2}, #] &]