Python add() function of set in list comprehension

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











up vote
1
down vote

favorite












I have come across the below code to remove duplicates from a list:



seen = set(); print [i for i in list if i not in seen and not seen.add(i)] 


I could not comprehend what exactly "and not seen.add(i)" this part of code is doing as help(set.add) gives below explanation:



add(...)
Add an element to a set.
This has no effect if the element is already present.


Looking forward to your help in understanding it







share|improve this question
























    up vote
    1
    down vote

    favorite












    I have come across the below code to remove duplicates from a list:



    seen = set(); print [i for i in list if i not in seen and not seen.add(i)] 


    I could not comprehend what exactly "and not seen.add(i)" this part of code is doing as help(set.add) gives below explanation:



    add(...)
    Add an element to a set.
    This has no effect if the element is already present.


    Looking forward to your help in understanding it







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have come across the below code to remove duplicates from a list:



      seen = set(); print [i for i in list if i not in seen and not seen.add(i)] 


      I could not comprehend what exactly "and not seen.add(i)" this part of code is doing as help(set.add) gives below explanation:



      add(...)
      Add an element to a set.
      This has no effect if the element is already present.


      Looking forward to your help in understanding it







      share|improve this question












      I have come across the below code to remove duplicates from a list:



      seen = set(); print [i for i in list if i not in seen and not seen.add(i)] 


      I could not comprehend what exactly "and not seen.add(i)" this part of code is doing as help(set.add) gives below explanation:



      add(...)
      Add an element to a set.
      This has no effect if the element is already present.


      Looking forward to your help in understanding it









      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '17 at 6:27









      Ibrahim Quraish

      1419




      1419




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.






          share|improve this answer
















          • 1




            Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
            – Ibrahim Quraish
            Nov 28 '17 at 7:04











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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%2funix.stackexchange.com%2fquestions%2f407424%2fpython-add-function-of-set-in-list-comprehension%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.






          share|improve this answer
















          • 1




            Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
            – Ibrahim Quraish
            Nov 28 '17 at 7:04















          up vote
          1
          down vote



          accepted










          The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.






          share|improve this answer
















          • 1




            Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
            – Ibrahim Quraish
            Nov 28 '17 at 7:04













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.






          share|improve this answer












          The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 '17 at 6:46









          igal

          4,830930




          4,830930







          • 1




            Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
            – Ibrahim Quraish
            Nov 28 '17 at 7:04













          • 1




            Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
            – Ibrahim Quraish
            Nov 28 '17 at 7:04








          1




          1




          Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
          – Ibrahim Quraish
          Nov 28 '17 at 7:04





          Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
          – Ibrahim Quraish
          Nov 28 '17 at 7:04


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407424%2fpython-add-function-of-set-in-list-comprehension%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay