Opposite of set.intersection in python?Solving 'is one away' with set()Get complement of three listsHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to know if an object has an attribute in PythonDoes Python have a string 'contains' substring method?Delete an element from a dictionaryMost elegant way to check if the string is empty in Python?

A ​Note ​on ​N!

Phrase for the opposite of "foolproof"

Do I have an "anti-research" personality?

What does ゆーか mean?

How did Captain America manage to do this?

How to stop co-workers from teasing me because I know Russian?

Checks user level and limit the data before saving it to mongoDB

Mistake in years of experience in resume?

How to have a sharp product image?

What term is being referred to with "reflected-sound-of-underground-spirits"?

Was there a shared-world project before "Thieves World"?

What is the philosophical significance of speech acts/implicature?

"The cow" OR "a cow" OR "cows" in this context

Relationship between strut and baselineskip

Check if a string is entirely made of the same substring

What happened to Captain America in Endgame?

Is it idiomatic to construct against `this`

bldc motor, esc and battery draw, nominal vs peak

Multiple options vs single option UI

What does the integral of a function times a function of a random variable represent, conceptually?

Elements other than carbon that can form many different compounds by bonding to themselves?

Rivers without rain

How do I check if a string is entirely made of the same substring?

Pulling the rope with one hand is as heavy as with two hands?



Opposite of set.intersection in python?


Solving 'is one away' with set()Get complement of three listsHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to know if an object has an attribute in PythonDoes Python have a string 'contains' substring method?Delete an element from a dictionaryMost elegant way to check if the string is empty in Python?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








40















In Python you can use a.intersection(b) to find the items common to both sets.



Is there a way to do the disjoint opposite version of this? Items that are not common to both a and b; the unique items in a unioned with the unique items in b?










share|improve this question






























    40















    In Python you can use a.intersection(b) to find the items common to both sets.



    Is there a way to do the disjoint opposite version of this? Items that are not common to both a and b; the unique items in a unioned with the unique items in b?










    share|improve this question


























      40












      40








      40


      6






      In Python you can use a.intersection(b) to find the items common to both sets.



      Is there a way to do the disjoint opposite version of this? Items that are not common to both a and b; the unique items in a unioned with the unique items in b?










      share|improve this question
















      In Python you can use a.intersection(b) to find the items common to both sets.



      Is there a way to do the disjoint opposite version of this? Items that are not common to both a and b; the unique items in a unioned with the unique items in b?







      python set






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 29 '15 at 15:18









      Martijn Pieters

      730k14625622361




      730k14625622361










      asked Apr 29 '15 at 15:13









      user4847061user4847061

      275137




      275137






















          5 Answers
          5






          active

          oldest

          votes


















          70














          You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:



          a.symmetric_difference(b)


          From the set.symmetric_difference() method documentation:




          Return a new set with elements in either the set or other but not both.




          You can use the ^ operator too, if both a and b are sets:



          a ^ b


          while set.symmetric_difference() takes any iterable for the other argument.



          The output is the equivalent of (a | b) - (a & b), the union of both sets minus the intersection of both sets.






          share|improve this answer

























          • Isn't ^ normaly XOR operator?

            – user4847061
            Apr 29 '15 at 15:31






          • 1





            @user4847061: it is, but sets have overloaded several such operators. | and & are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators <, <=, > and >= have been overloaded too.

            – Martijn Pieters
            Apr 29 '15 at 15:34


















          1














          a=1,2,4,5,6
          b=5,6,4,9
          c=(a^b)&b
          print(c) # you got 9





          share|improve this answer






























            1














            The best way is a list comprehension.



            a = [ 1,2,3,4]
            b = [ 8,7,9,2,1]
            c = [ element for element in a if element not in b]
            d = [ element for element in b if element not in a]
            print(c)
            # output is [ 3,4]
            print(d)
            # output is [8,7,9]


            You can join both lists






            share|improve this answer






























              0














              Try this code for (set(a) - intersection(a&b))



              a = [1,2,3,4,5,6]
              b = [2,3]

              for i in b:
              if i in a:
              a.remove(i)

              print(a)


              the output is [1,4,5,6]
              I hope, it will work






              share|improve this answer

























              • It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifying a). Also check = i in a is redundant since you can always if i in a:

                – cowbert
                Mar 14 '18 at 20:26












              • @cowbert thanks for your advise. I have fixed it. I will learn more about that.

                – Muhammad Ammar Fauzan
                Mar 16 '18 at 23:11


















              0














              e, f are two list you want to check disjoint



              a = [1,2,3,4]
              b = [8,7,9,2,1]

              c = []
              def loop_to_check(e,f):
              for i in range(len(e)):
              if e[i] not in f:
              c.append(e[i])


              loop_to_check(a,b)
              loop_to_check(b,a)
              print(c)

              ## output is [3,4,8,7,9]


              This loops around to list and returns the disjoint list






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                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',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29947844%2fopposite-of-set-intersection-in-python%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                70














                You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:



                a.symmetric_difference(b)


                From the set.symmetric_difference() method documentation:




                Return a new set with elements in either the set or other but not both.




                You can use the ^ operator too, if both a and b are sets:



                a ^ b


                while set.symmetric_difference() takes any iterable for the other argument.



                The output is the equivalent of (a | b) - (a & b), the union of both sets minus the intersection of both sets.






                share|improve this answer

























                • Isn't ^ normaly XOR operator?

                  – user4847061
                  Apr 29 '15 at 15:31






                • 1





                  @user4847061: it is, but sets have overloaded several such operators. | and & are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators <, <=, > and >= have been overloaded too.

                  – Martijn Pieters
                  Apr 29 '15 at 15:34















                70














                You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:



                a.symmetric_difference(b)


                From the set.symmetric_difference() method documentation:




                Return a new set with elements in either the set or other but not both.




                You can use the ^ operator too, if both a and b are sets:



                a ^ b


                while set.symmetric_difference() takes any iterable for the other argument.



                The output is the equivalent of (a | b) - (a & b), the union of both sets minus the intersection of both sets.






                share|improve this answer

























                • Isn't ^ normaly XOR operator?

                  – user4847061
                  Apr 29 '15 at 15:31






                • 1





                  @user4847061: it is, but sets have overloaded several such operators. | and & are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators <, <=, > and >= have been overloaded too.

                  – Martijn Pieters
                  Apr 29 '15 at 15:34













                70












                70








                70







                You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:



                a.symmetric_difference(b)


                From the set.symmetric_difference() method documentation:




                Return a new set with elements in either the set or other but not both.




                You can use the ^ operator too, if both a and b are sets:



                a ^ b


                while set.symmetric_difference() takes any iterable for the other argument.



                The output is the equivalent of (a | b) - (a & b), the union of both sets minus the intersection of both sets.






                share|improve this answer















                You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:



                a.symmetric_difference(b)


                From the set.symmetric_difference() method documentation:




                Return a new set with elements in either the set or other but not both.




                You can use the ^ operator too, if both a and b are sets:



                a ^ b


                while set.symmetric_difference() takes any iterable for the other argument.



                The output is the equivalent of (a | b) - (a & b), the union of both sets minus the intersection of both sets.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 9 '17 at 14:27

























                answered Apr 29 '15 at 15:15









                Martijn PietersMartijn Pieters

                730k14625622361




                730k14625622361












                • Isn't ^ normaly XOR operator?

                  – user4847061
                  Apr 29 '15 at 15:31






                • 1





                  @user4847061: it is, but sets have overloaded several such operators. | and & are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators <, <=, > and >= have been overloaded too.

                  – Martijn Pieters
                  Apr 29 '15 at 15:34

















                • Isn't ^ normaly XOR operator?

                  – user4847061
                  Apr 29 '15 at 15:31






                • 1





                  @user4847061: it is, but sets have overloaded several such operators. | and & are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators <, <=, > and >= have been overloaded too.

                  – Martijn Pieters
                  Apr 29 '15 at 15:34
















                Isn't ^ normaly XOR operator?

                – user4847061
                Apr 29 '15 at 15:31





                Isn't ^ normaly XOR operator?

                – user4847061
                Apr 29 '15 at 15:31




                1




                1





                @user4847061: it is, but sets have overloaded several such operators. | and & are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators <, <=, > and >= have been overloaded too.

                – Martijn Pieters
                Apr 29 '15 at 15:34





                @user4847061: it is, but sets have overloaded several such operators. | and & are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators <, <=, > and >= have been overloaded too.

                – Martijn Pieters
                Apr 29 '15 at 15:34













                1














                a=1,2,4,5,6
                b=5,6,4,9
                c=(a^b)&b
                print(c) # you got 9





                share|improve this answer



























                  1














                  a=1,2,4,5,6
                  b=5,6,4,9
                  c=(a^b)&b
                  print(c) # you got 9





                  share|improve this answer

























                    1












                    1








                    1







                    a=1,2,4,5,6
                    b=5,6,4,9
                    c=(a^b)&b
                    print(c) # you got 9





                    share|improve this answer













                    a=1,2,4,5,6
                    b=5,6,4,9
                    c=(a^b)&b
                    print(c) # you got 9






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 28 '18 at 4:39









                    Kaung Yar ZarKaung Yar Zar

                    111




                    111





















                        1














                        The best way is a list comprehension.



                        a = [ 1,2,3,4]
                        b = [ 8,7,9,2,1]
                        c = [ element for element in a if element not in b]
                        d = [ element for element in b if element not in a]
                        print(c)
                        # output is [ 3,4]
                        print(d)
                        # output is [8,7,9]


                        You can join both lists






                        share|improve this answer



























                          1














                          The best way is a list comprehension.



                          a = [ 1,2,3,4]
                          b = [ 8,7,9,2,1]
                          c = [ element for element in a if element not in b]
                          d = [ element for element in b if element not in a]
                          print(c)
                          # output is [ 3,4]
                          print(d)
                          # output is [8,7,9]


                          You can join both lists






                          share|improve this answer

























                            1












                            1








                            1







                            The best way is a list comprehension.



                            a = [ 1,2,3,4]
                            b = [ 8,7,9,2,1]
                            c = [ element for element in a if element not in b]
                            d = [ element for element in b if element not in a]
                            print(c)
                            # output is [ 3,4]
                            print(d)
                            # output is [8,7,9]


                            You can join both lists






                            share|improve this answer













                            The best way is a list comprehension.



                            a = [ 1,2,3,4]
                            b = [ 8,7,9,2,1]
                            c = [ element for element in a if element not in b]
                            d = [ element for element in b if element not in a]
                            print(c)
                            # output is [ 3,4]
                            print(d)
                            # output is [8,7,9]


                            You can join both lists







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 22 at 17:24









                            ApeasantApeasant

                            213




                            213





















                                0














                                Try this code for (set(a) - intersection(a&b))



                                a = [1,2,3,4,5,6]
                                b = [2,3]

                                for i in b:
                                if i in a:
                                a.remove(i)

                                print(a)


                                the output is [1,4,5,6]
                                I hope, it will work






                                share|improve this answer

























                                • It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifying a). Also check = i in a is redundant since you can always if i in a:

                                  – cowbert
                                  Mar 14 '18 at 20:26












                                • @cowbert thanks for your advise. I have fixed it. I will learn more about that.

                                  – Muhammad Ammar Fauzan
                                  Mar 16 '18 at 23:11















                                0














                                Try this code for (set(a) - intersection(a&b))



                                a = [1,2,3,4,5,6]
                                b = [2,3]

                                for i in b:
                                if i in a:
                                a.remove(i)

                                print(a)


                                the output is [1,4,5,6]
                                I hope, it will work






                                share|improve this answer

























                                • It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifying a). Also check = i in a is redundant since you can always if i in a:

                                  – cowbert
                                  Mar 14 '18 at 20:26












                                • @cowbert thanks for your advise. I have fixed it. I will learn more about that.

                                  – Muhammad Ammar Fauzan
                                  Mar 16 '18 at 23:11













                                0












                                0








                                0







                                Try this code for (set(a) - intersection(a&b))



                                a = [1,2,3,4,5,6]
                                b = [2,3]

                                for i in b:
                                if i in a:
                                a.remove(i)

                                print(a)


                                the output is [1,4,5,6]
                                I hope, it will work






                                share|improve this answer















                                Try this code for (set(a) - intersection(a&b))



                                a = [1,2,3,4,5,6]
                                b = [2,3]

                                for i in b:
                                if i in a:
                                a.remove(i)

                                print(a)


                                the output is [1,4,5,6]
                                I hope, it will work







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Mar 16 '18 at 23:09

























                                answered Mar 11 '18 at 11:00









                                Muhammad Ammar FauzanMuhammad Ammar Fauzan

                                614




                                614












                                • It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifying a). Also check = i in a is redundant since you can always if i in a:

                                  – cowbert
                                  Mar 14 '18 at 20:26












                                • @cowbert thanks for your advise. I have fixed it. I will learn more about that.

                                  – Muhammad Ammar Fauzan
                                  Mar 16 '18 at 23:11

















                                • It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifying a). Also check = i in a is redundant since you can always if i in a:

                                  – cowbert
                                  Mar 14 '18 at 20:26












                                • @cowbert thanks for your advise. I have fixed it. I will learn more about that.

                                  – Muhammad Ammar Fauzan
                                  Mar 16 '18 at 23:11
















                                It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifying a). Also check = i in a is redundant since you can always if i in a:

                                – cowbert
                                Mar 14 '18 at 20:26






                                It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifying a). Also check = i in a is redundant since you can always if i in a:

                                – cowbert
                                Mar 14 '18 at 20:26














                                @cowbert thanks for your advise. I have fixed it. I will learn more about that.

                                – Muhammad Ammar Fauzan
                                Mar 16 '18 at 23:11





                                @cowbert thanks for your advise. I have fixed it. I will learn more about that.

                                – Muhammad Ammar Fauzan
                                Mar 16 '18 at 23:11











                                0














                                e, f are two list you want to check disjoint



                                a = [1,2,3,4]
                                b = [8,7,9,2,1]

                                c = []
                                def loop_to_check(e,f):
                                for i in range(len(e)):
                                if e[i] not in f:
                                c.append(e[i])


                                loop_to_check(a,b)
                                loop_to_check(b,a)
                                print(c)

                                ## output is [3,4,8,7,9]


                                This loops around to list and returns the disjoint list






                                share|improve this answer



























                                  0














                                  e, f are two list you want to check disjoint



                                  a = [1,2,3,4]
                                  b = [8,7,9,2,1]

                                  c = []
                                  def loop_to_check(e,f):
                                  for i in range(len(e)):
                                  if e[i] not in f:
                                  c.append(e[i])


                                  loop_to_check(a,b)
                                  loop_to_check(b,a)
                                  print(c)

                                  ## output is [3,4,8,7,9]


                                  This loops around to list and returns the disjoint list






                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    e, f are two list you want to check disjoint



                                    a = [1,2,3,4]
                                    b = [8,7,9,2,1]

                                    c = []
                                    def loop_to_check(e,f):
                                    for i in range(len(e)):
                                    if e[i] not in f:
                                    c.append(e[i])


                                    loop_to_check(a,b)
                                    loop_to_check(b,a)
                                    print(c)

                                    ## output is [3,4,8,7,9]


                                    This loops around to list and returns the disjoint list






                                    share|improve this answer













                                    e, f are two list you want to check disjoint



                                    a = [1,2,3,4]
                                    b = [8,7,9,2,1]

                                    c = []
                                    def loop_to_check(e,f):
                                    for i in range(len(e)):
                                    if e[i] not in f:
                                    c.append(e[i])


                                    loop_to_check(a,b)
                                    loop_to_check(b,a)
                                    print(c)

                                    ## output is [3,4,8,7,9]


                                    This loops around to list and returns the disjoint list







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 31 '18 at 5:16









                                    chandra singhchandra singh

                                    133




                                    133



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29947844%2fopposite-of-set-intersection-in-python%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                        Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                        Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript