Deleting specific elements in a list when they come together

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












Suppose I have the following list



l="x","b","c","y","a","d","x","b","y","x","y","c"


I want to go through the list l and delete elements whenever "x" and "y" appear both in a single sublist such that I get:



"x","b","c","y","a","d"


Order does not matter, as long as "x" and "y" both exist.







share|improve this question
















  • 2




    Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
    – rhermans
    Aug 9 at 17:02














up vote
4
down vote

favorite












Suppose I have the following list



l="x","b","c","y","a","d","x","b","y","x","y","c"


I want to go through the list l and delete elements whenever "x" and "y" appear both in a single sublist such that I get:



"x","b","c","y","a","d"


Order does not matter, as long as "x" and "y" both exist.







share|improve this question
















  • 2




    Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
    – rhermans
    Aug 9 at 17:02












up vote
4
down vote

favorite









up vote
4
down vote

favorite











Suppose I have the following list



l="x","b","c","y","a","d","x","b","y","x","y","c"


I want to go through the list l and delete elements whenever "x" and "y" appear both in a single sublist such that I get:



"x","b","c","y","a","d"


Order does not matter, as long as "x" and "y" both exist.







share|improve this question












Suppose I have the following list



l="x","b","c","y","a","d","x","b","y","x","y","c"


I want to go through the list l and delete elements whenever "x" and "y" appear both in a single sublist such that I get:



"x","b","c","y","a","d"


Order does not matter, as long as "x" and "y" both exist.









share|improve this question











share|improve this question




share|improve this question










asked Aug 9 at 16:34









William

35517




35517







  • 2




    Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
    – rhermans
    Aug 9 at 17:02












  • 2




    Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
    – rhermans
    Aug 9 at 17:02







2




2




Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
– rhermans
Aug 9 at 17:02




Thanks for accepting an answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
– rhermans
Aug 9 at 17:02










3 Answers
3






active

oldest

votes

















up vote
5
down vote



accepted










Something like the following?



l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]

(* Out: “x”, “b”, “c”, “y”, “a”, “d” *)





share|improve this answer






















  • Thank you for this.
    – William
    Aug 9 at 16:49










  • @William you are very welcome.
    – MarcoB
    Aug 9 at 17:20










  • @William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
    – user202729
    Aug 10 at 3:00

















up vote
6
down vote













DeleteCases[l, _?(ContainsAll["x", "y"])]



"x", "b", "c", "y", "a", "d"







share|improve this answer



























    up vote
    5
    down vote













    A different solution using SubsetQ and Select. I also prefer ti limit the scope of the variable definitions using With or Module.



    With[

    l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
    ,
    Select[l, Not[SubsetQ[#, "x", "y"]] &]
    ]





    share|improve this answer


















    • 2




      It's not documented, but SubsetQ supports an operator form, so you could eliminate the pure function and instead do Select[l, Not @* SubsetQ["x", "y"]]
      – Carl Woll
      Aug 9 at 18:07






    • 1




      @CarlWoll Nice tip. I really wish ! SubsetQ["x", "y"] would work however.
      – Mr.Wizard♦
      Aug 9 at 21:44










    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "387"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f179765%2fdeleting-specific-elements-in-a-list-when-they-come-together%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    Something like the following?



    l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
    DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]

    (* Out: “x”, “b”, “c”, “y”, “a”, “d” *)





    share|improve this answer






















    • Thank you for this.
      – William
      Aug 9 at 16:49










    • @William you are very welcome.
      – MarcoB
      Aug 9 at 17:20










    • @William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
      – user202729
      Aug 10 at 3:00














    up vote
    5
    down vote



    accepted










    Something like the following?



    l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
    DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]

    (* Out: “x”, “b”, “c”, “y”, “a”, “d” *)





    share|improve this answer






















    • Thank you for this.
      – William
      Aug 9 at 16:49










    • @William you are very welcome.
      – MarcoB
      Aug 9 at 17:20










    • @William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
      – user202729
      Aug 10 at 3:00












    up vote
    5
    down vote



    accepted







    up vote
    5
    down vote



    accepted






    Something like the following?



    l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
    DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]

    (* Out: “x”, “b”, “c”, “y”, “a”, “d” *)





    share|improve this answer














    Something like the following?



    l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c";
    DeleteCases[l, OrderlessPatternSequence["x", "y", ___]]

    (* Out: “x”, “b”, “c”, “y”, “a”, “d” *)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 9 at 21:43









    Mr.Wizard♦

    227k284631014




    227k284631014










    answered Aug 9 at 16:47









    MarcoB

    35.7k556111




    35.7k556111











    • Thank you for this.
      – William
      Aug 9 at 16:49










    • @William you are very welcome.
      – MarcoB
      Aug 9 at 17:20










    • @William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
      – user202729
      Aug 10 at 3:00
















    • Thank you for this.
      – William
      Aug 9 at 16:49










    • @William you are very welcome.
      – MarcoB
      Aug 9 at 17:20










    • @William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
      – user202729
      Aug 10 at 3:00















    Thank you for this.
    – William
    Aug 9 at 16:49




    Thank you for this.
    – William
    Aug 9 at 16:49












    @William you are very welcome.
    – MarcoB
    Aug 9 at 17:20




    @William you are very welcome.
    – MarcoB
    Aug 9 at 17:20












    @William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
    – user202729
    Aug 10 at 3:00




    @William Upvoting and accepting suffices. Comments are only for improvement suggestions, not "private messaging".
    – user202729
    Aug 10 at 3:00










    up vote
    6
    down vote













    DeleteCases[l, _?(ContainsAll["x", "y"])]



    "x", "b", "c", "y", "a", "d"







    share|improve this answer
























      up vote
      6
      down vote













      DeleteCases[l, _?(ContainsAll["x", "y"])]



      "x", "b", "c", "y", "a", "d"







      share|improve this answer






















        up vote
        6
        down vote










        up vote
        6
        down vote









        DeleteCases[l, _?(ContainsAll["x", "y"])]



        "x", "b", "c", "y", "a", "d"







        share|improve this answer












        DeleteCases[l, _?(ContainsAll["x", "y"])]



        "x", "b", "c", "y", "a", "d"








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 9 at 19:36









        kglr

        157k8182379




        157k8182379




















            up vote
            5
            down vote













            A different solution using SubsetQ and Select. I also prefer ti limit the scope of the variable definitions using With or Module.



            With[

            l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
            ,
            Select[l, Not[SubsetQ[#, "x", "y"]] &]
            ]





            share|improve this answer


















            • 2




              It's not documented, but SubsetQ supports an operator form, so you could eliminate the pure function and instead do Select[l, Not @* SubsetQ["x", "y"]]
              – Carl Woll
              Aug 9 at 18:07






            • 1




              @CarlWoll Nice tip. I really wish ! SubsetQ["x", "y"] would work however.
              – Mr.Wizard♦
              Aug 9 at 21:44














            up vote
            5
            down vote













            A different solution using SubsetQ and Select. I also prefer ti limit the scope of the variable definitions using With or Module.



            With[

            l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
            ,
            Select[l, Not[SubsetQ[#, "x", "y"]] &]
            ]





            share|improve this answer


















            • 2




              It's not documented, but SubsetQ supports an operator form, so you could eliminate the pure function and instead do Select[l, Not @* SubsetQ["x", "y"]]
              – Carl Woll
              Aug 9 at 18:07






            • 1




              @CarlWoll Nice tip. I really wish ! SubsetQ["x", "y"] would work however.
              – Mr.Wizard♦
              Aug 9 at 21:44












            up vote
            5
            down vote










            up vote
            5
            down vote









            A different solution using SubsetQ and Select. I also prefer ti limit the scope of the variable definitions using With or Module.



            With[

            l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
            ,
            Select[l, Not[SubsetQ[#, "x", "y"]] &]
            ]





            share|improve this answer














            A different solution using SubsetQ and Select. I also prefer ti limit the scope of the variable definitions using With or Module.



            With[

            l = "x", "b", "c", "y", "a", "d", "x", "b", "y", "x", "y", "c"
            ,
            Select[l, Not[SubsetQ[#, "x", "y"]] &]
            ]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 9 at 17:09

























            answered Aug 9 at 17:01









            rhermans

            21.6k439103




            21.6k439103







            • 2




              It's not documented, but SubsetQ supports an operator form, so you could eliminate the pure function and instead do Select[l, Not @* SubsetQ["x", "y"]]
              – Carl Woll
              Aug 9 at 18:07






            • 1




              @CarlWoll Nice tip. I really wish ! SubsetQ["x", "y"] would work however.
              – Mr.Wizard♦
              Aug 9 at 21:44












            • 2




              It's not documented, but SubsetQ supports an operator form, so you could eliminate the pure function and instead do Select[l, Not @* SubsetQ["x", "y"]]
              – Carl Woll
              Aug 9 at 18:07






            • 1




              @CarlWoll Nice tip. I really wish ! SubsetQ["x", "y"] would work however.
              – Mr.Wizard♦
              Aug 9 at 21:44







            2




            2




            It's not documented, but SubsetQ supports an operator form, so you could eliminate the pure function and instead do Select[l, Not @* SubsetQ["x", "y"]]
            – Carl Woll
            Aug 9 at 18:07




            It's not documented, but SubsetQ supports an operator form, so you could eliminate the pure function and instead do Select[l, Not @* SubsetQ["x", "y"]]
            – Carl Woll
            Aug 9 at 18:07




            1




            1




            @CarlWoll Nice tip. I really wish ! SubsetQ["x", "y"] would work however.
            – Mr.Wizard♦
            Aug 9 at 21:44




            @CarlWoll Nice tip. I really wish ! SubsetQ["x", "y"] would work however.
            – Mr.Wizard♦
            Aug 9 at 21:44

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f179765%2fdeleting-specific-elements-in-a-list-when-they-come-together%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery