Python - Print only when something gets added into a list












0















So I got an idea where you have a list etc of [1,2,3,4,5] and my idea was that incase a number of those gets pop/deleted then it shouldn't print. let's say etc. we remove number 3. In that case our list would be [1,2,4,5] and the script should be usual. But whenever a value gets added to a list. Then print out the whole list so etc. add number 6 --> [1,2,4,5,6] - Print out the whole list.



The problem is that I don't want to get notified whenever there has been anything deleted so my idea from the beginning was to check the length of a list and then notify whenever it gets changed but then I realized that the wrong I am doing is that it gonna notify whenever it gets added and/nor deleted which now why I am here.



What I did was a compare of a new_name_list vs old_name_list but this one is gonna notify for whatever change that would be happens basically.



import names


def get_value(value):

value = names.get_full_name()

names_list =
for names in names.get_last_name():
names_list.append(names)
break

identifier = ('{} {}').format(value, names_list)

return identifier


if __name__ == '__main__':

old_name_list = get_value()

while True:
new_name_list = get_value()
if new_name_list not in old_name_list:
print("Yay new name added")

else:
print('I will re try again in 5 sec')
time.sleep(5)


My question is - How can I make it so it print only whenever the value of names_list will get notified ONLY when something gets added but not deleted?



etc.




  1. [1,2,3,4,5] - print from beginning

  2. [1,2,4,5] - Deleted 3 - Do not print

  3. [1,2,4,5,6] - Print list, something got added

  4. [1,4,5,6] - Deleted 2 - Do not print

  5. .........


I hope I did explain it correct and if there is anything misunderstanding. Please let me know and I will try to edit as fast as possible and also to learn to write better aswell for next time (Any tip will be appreciated :) )










share|improve this question





























    0















    So I got an idea where you have a list etc of [1,2,3,4,5] and my idea was that incase a number of those gets pop/deleted then it shouldn't print. let's say etc. we remove number 3. In that case our list would be [1,2,4,5] and the script should be usual. But whenever a value gets added to a list. Then print out the whole list so etc. add number 6 --> [1,2,4,5,6] - Print out the whole list.



    The problem is that I don't want to get notified whenever there has been anything deleted so my idea from the beginning was to check the length of a list and then notify whenever it gets changed but then I realized that the wrong I am doing is that it gonna notify whenever it gets added and/nor deleted which now why I am here.



    What I did was a compare of a new_name_list vs old_name_list but this one is gonna notify for whatever change that would be happens basically.



    import names


    def get_value(value):

    value = names.get_full_name()

    names_list =
    for names in names.get_last_name():
    names_list.append(names)
    break

    identifier = ('{} {}').format(value, names_list)

    return identifier


    if __name__ == '__main__':

    old_name_list = get_value()

    while True:
    new_name_list = get_value()
    if new_name_list not in old_name_list:
    print("Yay new name added")

    else:
    print('I will re try again in 5 sec')
    time.sleep(5)


    My question is - How can I make it so it print only whenever the value of names_list will get notified ONLY when something gets added but not deleted?



    etc.




    1. [1,2,3,4,5] - print from beginning

    2. [1,2,4,5] - Deleted 3 - Do not print

    3. [1,2,4,5,6] - Print list, something got added

    4. [1,4,5,6] - Deleted 2 - Do not print

    5. .........


    I hope I did explain it correct and if there is anything misunderstanding. Please let me know and I will try to edit as fast as possible and also to learn to write better aswell for next time (Any tip will be appreciated :) )










    share|improve this question



























      0












      0








      0








      So I got an idea where you have a list etc of [1,2,3,4,5] and my idea was that incase a number of those gets pop/deleted then it shouldn't print. let's say etc. we remove number 3. In that case our list would be [1,2,4,5] and the script should be usual. But whenever a value gets added to a list. Then print out the whole list so etc. add number 6 --> [1,2,4,5,6] - Print out the whole list.



      The problem is that I don't want to get notified whenever there has been anything deleted so my idea from the beginning was to check the length of a list and then notify whenever it gets changed but then I realized that the wrong I am doing is that it gonna notify whenever it gets added and/nor deleted which now why I am here.



      What I did was a compare of a new_name_list vs old_name_list but this one is gonna notify for whatever change that would be happens basically.



      import names


      def get_value(value):

      value = names.get_full_name()

      names_list =
      for names in names.get_last_name():
      names_list.append(names)
      break

      identifier = ('{} {}').format(value, names_list)

      return identifier


      if __name__ == '__main__':

      old_name_list = get_value()

      while True:
      new_name_list = get_value()
      if new_name_list not in old_name_list:
      print("Yay new name added")

      else:
      print('I will re try again in 5 sec')
      time.sleep(5)


      My question is - How can I make it so it print only whenever the value of names_list will get notified ONLY when something gets added but not deleted?



      etc.




      1. [1,2,3,4,5] - print from beginning

      2. [1,2,4,5] - Deleted 3 - Do not print

      3. [1,2,4,5,6] - Print list, something got added

      4. [1,4,5,6] - Deleted 2 - Do not print

      5. .........


      I hope I did explain it correct and if there is anything misunderstanding. Please let me know and I will try to edit as fast as possible and also to learn to write better aswell for next time (Any tip will be appreciated :) )










      share|improve this question
















      So I got an idea where you have a list etc of [1,2,3,4,5] and my idea was that incase a number of those gets pop/deleted then it shouldn't print. let's say etc. we remove number 3. In that case our list would be [1,2,4,5] and the script should be usual. But whenever a value gets added to a list. Then print out the whole list so etc. add number 6 --> [1,2,4,5,6] - Print out the whole list.



      The problem is that I don't want to get notified whenever there has been anything deleted so my idea from the beginning was to check the length of a list and then notify whenever it gets changed but then I realized that the wrong I am doing is that it gonna notify whenever it gets added and/nor deleted which now why I am here.



      What I did was a compare of a new_name_list vs old_name_list but this one is gonna notify for whatever change that would be happens basically.



      import names


      def get_value(value):

      value = names.get_full_name()

      names_list =
      for names in names.get_last_name():
      names_list.append(names)
      break

      identifier = ('{} {}').format(value, names_list)

      return identifier


      if __name__ == '__main__':

      old_name_list = get_value()

      while True:
      new_name_list = get_value()
      if new_name_list not in old_name_list:
      print("Yay new name added")

      else:
      print('I will re try again in 5 sec')
      time.sleep(5)


      My question is - How can I make it so it print only whenever the value of names_list will get notified ONLY when something gets added but not deleted?



      etc.




      1. [1,2,3,4,5] - print from beginning

      2. [1,2,4,5] - Deleted 3 - Do not print

      3. [1,2,4,5,6] - Print list, something got added

      4. [1,4,5,6] - Deleted 2 - Do not print

      5. .........


      I hope I did explain it correct and if there is anything misunderstanding. Please let me know and I will try to edit as fast as possible and also to learn to write better aswell for next time (Any tip will be appreciated :) )







      python list printing add pop






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 22:09









      Adam

      35710




      35710










      asked Nov 20 '18 at 21:57









      HellosiroverthereHellosiroverthere

      11718




      11718
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Algorithm could be the following:




          1. Order old and new lists.

          2. If length is equal -> compare element-to-element. If new list contains different elements than old list, means new values were added.

          3. If new list length > old list length, also means, that new elements were added.


          Example:



          def detect_change(old_list, new_list):
          changed_flag = False
          old_list.sort()
          new_list.sort()
          if len(old_list) == len(new_list):
          for i in range(0, len(old_list)):
          if old_list[i] != new_list[i]:
          changed_flag = True
          elif len(old_list) < len(new_list):
          changed_flag = True
          return changed_flag

          list1 = ["a", "b", "c", "d"]
          list2 = ["a", "b", "c", "k"]

          print(detect_change(list1, list2))





          share|improve this answer


























          • Hmm alright - I can't really get anything through my head about the algorithm you explained. I kidna get it but do you mind do a sort of coding example where I can understand better out of it?

            – Hellosiroverthere
            Nov 20 '18 at 22:28











          • Please, check the updated answer - I've added code example for it

            – kosist
            Nov 21 '18 at 6:52











          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%2f53402219%2fpython-print-only-when-something-gets-added-into-a-list%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Algorithm could be the following:




          1. Order old and new lists.

          2. If length is equal -> compare element-to-element. If new list contains different elements than old list, means new values were added.

          3. If new list length > old list length, also means, that new elements were added.


          Example:



          def detect_change(old_list, new_list):
          changed_flag = False
          old_list.sort()
          new_list.sort()
          if len(old_list) == len(new_list):
          for i in range(0, len(old_list)):
          if old_list[i] != new_list[i]:
          changed_flag = True
          elif len(old_list) < len(new_list):
          changed_flag = True
          return changed_flag

          list1 = ["a", "b", "c", "d"]
          list2 = ["a", "b", "c", "k"]

          print(detect_change(list1, list2))





          share|improve this answer


























          • Hmm alright - I can't really get anything through my head about the algorithm you explained. I kidna get it but do you mind do a sort of coding example where I can understand better out of it?

            – Hellosiroverthere
            Nov 20 '18 at 22:28











          • Please, check the updated answer - I've added code example for it

            – kosist
            Nov 21 '18 at 6:52
















          1














          Algorithm could be the following:




          1. Order old and new lists.

          2. If length is equal -> compare element-to-element. If new list contains different elements than old list, means new values were added.

          3. If new list length > old list length, also means, that new elements were added.


          Example:



          def detect_change(old_list, new_list):
          changed_flag = False
          old_list.sort()
          new_list.sort()
          if len(old_list) == len(new_list):
          for i in range(0, len(old_list)):
          if old_list[i] != new_list[i]:
          changed_flag = True
          elif len(old_list) < len(new_list):
          changed_flag = True
          return changed_flag

          list1 = ["a", "b", "c", "d"]
          list2 = ["a", "b", "c", "k"]

          print(detect_change(list1, list2))





          share|improve this answer


























          • Hmm alright - I can't really get anything through my head about the algorithm you explained. I kidna get it but do you mind do a sort of coding example where I can understand better out of it?

            – Hellosiroverthere
            Nov 20 '18 at 22:28











          • Please, check the updated answer - I've added code example for it

            – kosist
            Nov 21 '18 at 6:52














          1












          1








          1







          Algorithm could be the following:




          1. Order old and new lists.

          2. If length is equal -> compare element-to-element. If new list contains different elements than old list, means new values were added.

          3. If new list length > old list length, also means, that new elements were added.


          Example:



          def detect_change(old_list, new_list):
          changed_flag = False
          old_list.sort()
          new_list.sort()
          if len(old_list) == len(new_list):
          for i in range(0, len(old_list)):
          if old_list[i] != new_list[i]:
          changed_flag = True
          elif len(old_list) < len(new_list):
          changed_flag = True
          return changed_flag

          list1 = ["a", "b", "c", "d"]
          list2 = ["a", "b", "c", "k"]

          print(detect_change(list1, list2))





          share|improve this answer















          Algorithm could be the following:




          1. Order old and new lists.

          2. If length is equal -> compare element-to-element. If new list contains different elements than old list, means new values were added.

          3. If new list length > old list length, also means, that new elements were added.


          Example:



          def detect_change(old_list, new_list):
          changed_flag = False
          old_list.sort()
          new_list.sort()
          if len(old_list) == len(new_list):
          for i in range(0, len(old_list)):
          if old_list[i] != new_list[i]:
          changed_flag = True
          elif len(old_list) < len(new_list):
          changed_flag = True
          return changed_flag

          list1 = ["a", "b", "c", "d"]
          list2 = ["a", "b", "c", "k"]

          print(detect_change(list1, list2))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 6:51

























          answered Nov 20 '18 at 22:03









          kosistkosist

          5271320




          5271320













          • Hmm alright - I can't really get anything through my head about the algorithm you explained. I kidna get it but do you mind do a sort of coding example where I can understand better out of it?

            – Hellosiroverthere
            Nov 20 '18 at 22:28











          • Please, check the updated answer - I've added code example for it

            – kosist
            Nov 21 '18 at 6:52



















          • Hmm alright - I can't really get anything through my head about the algorithm you explained. I kidna get it but do you mind do a sort of coding example where I can understand better out of it?

            – Hellosiroverthere
            Nov 20 '18 at 22:28











          • Please, check the updated answer - I've added code example for it

            – kosist
            Nov 21 '18 at 6:52

















          Hmm alright - I can't really get anything through my head about the algorithm you explained. I kidna get it but do you mind do a sort of coding example where I can understand better out of it?

          – Hellosiroverthere
          Nov 20 '18 at 22:28





          Hmm alright - I can't really get anything through my head about the algorithm you explained. I kidna get it but do you mind do a sort of coding example where I can understand better out of it?

          – Hellosiroverthere
          Nov 20 '18 at 22:28













          Please, check the updated answer - I've added code example for it

          – kosist
          Nov 21 '18 at 6:52





          Please, check the updated answer - I've added code example for it

          – kosist
          Nov 21 '18 at 6:52




















          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%2f53402219%2fpython-print-only-when-something-gets-added-into-a-list%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

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?