I have a nested list where all sublists have a length 2, eg:
{{1,2}, {1,1}, {3,4}, {3,5}, ...}
I need to filter it for two requirements:
- Delete all sub-lists with the same
First
(except the first such sub-list) - Delete all sub-lists with
First
equal toLast
I get a huge construction:
DeleteDuplicates[
DeleteCases[list, e_ /; First@e == Last@e],
First@#1 == First@#2 &
]
Could it be golfed, for example, including union of duplicates in DeleteCases
?
DeleteDuplicatesBy[..., First]
instead ofDeleteDuplicates[..., First@#1 == First@#2 &]
. $\endgroup$DeleteDuplicatesBy
! But may be is it possible to do all filtering with one function? $\endgroup$DeleteDuplicatesBy[DeleteCases[{a_, a_}]@list, First]
$\endgroup$DeleteDuplicates[list, First@#1 == First@#2 || First@#1 == Last@#1 || First@#2 == Last@#2 &]
? $\endgroup$del = DeleteDuplicatesBy[list, First];
$$ $$Select[del, ! Equal @@ # &]
$$ $$ is short and readable, and also gives good performance for large lists. $\endgroup$