How to delete list item automatically in python?












8















If i have a list
for example :
num=[1,2,3,4]
and i want to delete
list[0]
every 2 second, so after 2 second list in num is num = [2,3,4] and 2 second after it will be num =[2,3]
how can i do that?










share|improve this question





























    8















    If i have a list
    for example :
    num=[1,2,3,4]
    and i want to delete
    list[0]
    every 2 second, so after 2 second list in num is num = [2,3,4] and 2 second after it will be num =[2,3]
    how can i do that?










    share|improve this question



























      8












      8








      8


      1






      If i have a list
      for example :
      num=[1,2,3,4]
      and i want to delete
      list[0]
      every 2 second, so after 2 second list in num is num = [2,3,4] and 2 second after it will be num =[2,3]
      how can i do that?










      share|improve this question
















      If i have a list
      for example :
      num=[1,2,3,4]
      and i want to delete
      list[0]
      every 2 second, so after 2 second list in num is num = [2,3,4] and 2 second after it will be num =[2,3]
      how can i do that?







      python






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 27 '18 at 15:40









      yatu

      6,8061826




      6,8061826










      asked Dec 27 '18 at 15:32









      TiffanyTiffany

      556




      556
























          4 Answers
          4






          active

          oldest

          votes


















          9














          You could do it using time.sleep, and del to remove the first element:



          for i in range(len(num)):
          time.sleep(2)
          del num[0]





          share|improve this answer
























          • Additionally, if you also want to do something with that first value, you could use num.pop(0)

            – KuboMD
            Dec 27 '18 at 15:58






          • 1





            Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque

            – Gallaecio
            Dec 30 '18 at 10:57



















          4














          Another solution is using list.pop(), as follows:



          import time
          num=[1,2,3,4]
          while num:
          num.pop(0)
          time.sleep(2)





          share|improve this answer

































            3














            This does so, until eventually it runs out of elements to remove.



            import time

            num = [1, 2, 3, 4]
            while num:
            time.sleep(2)
            del num[0]
            print (num)





            share|improve this answer





















            • 7





              Might be tempted to make that while num instead of while True - that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.

              – Jon Clements
              Dec 27 '18 at 15:41













            • Good idea. Done

              – Joshua Fox
              Dec 27 '18 at 19:19



















            0














            Try this code !



            Use sleep function by importing time file & del command to remove the element from list.



            Code :



            import time

            num = [1,2,3,4]
            for i in num:
            time.sleep(2)
            print("Delete item ", i)
            del i


            Output :



            Delete item  1                                                                                                          
            Delete item 2
            Delete item 3
            Delete item 4





            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%2f53947391%2fhow-to-delete-list-item-automatically-in-python%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              9














              You could do it using time.sleep, and del to remove the first element:



              for i in range(len(num)):
              time.sleep(2)
              del num[0]





              share|improve this answer
























              • Additionally, if you also want to do something with that first value, you could use num.pop(0)

                – KuboMD
                Dec 27 '18 at 15:58






              • 1





                Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque

                – Gallaecio
                Dec 30 '18 at 10:57
















              9














              You could do it using time.sleep, and del to remove the first element:



              for i in range(len(num)):
              time.sleep(2)
              del num[0]





              share|improve this answer
























              • Additionally, if you also want to do something with that first value, you could use num.pop(0)

                – KuboMD
                Dec 27 '18 at 15:58






              • 1





                Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque

                – Gallaecio
                Dec 30 '18 at 10:57














              9












              9








              9







              You could do it using time.sleep, and del to remove the first element:



              for i in range(len(num)):
              time.sleep(2)
              del num[0]





              share|improve this answer













              You could do it using time.sleep, and del to remove the first element:



              for i in range(len(num)):
              time.sleep(2)
              del num[0]






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 27 '18 at 15:35









              yatuyatu

              6,8061826




              6,8061826













              • Additionally, if you also want to do something with that first value, you could use num.pop(0)

                – KuboMD
                Dec 27 '18 at 15:58






              • 1





                Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque

                – Gallaecio
                Dec 30 '18 at 10:57



















              • Additionally, if you also want to do something with that first value, you could use num.pop(0)

                – KuboMD
                Dec 27 '18 at 15:58






              • 1





                Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque

                – Gallaecio
                Dec 30 '18 at 10:57

















              Additionally, if you also want to do something with that first value, you could use num.pop(0)

              – KuboMD
              Dec 27 '18 at 15:58





              Additionally, if you also want to do something with that first value, you could use num.pop(0)

              – KuboMD
              Dec 27 '18 at 15:58




              1




              1





              Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque

              – Gallaecio
              Dec 30 '18 at 10:57





              Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque

              – Gallaecio
              Dec 30 '18 at 10:57













              4














              Another solution is using list.pop(), as follows:



              import time
              num=[1,2,3,4]
              while num:
              num.pop(0)
              time.sleep(2)





              share|improve this answer






























                4














                Another solution is using list.pop(), as follows:



                import time
                num=[1,2,3,4]
                while num:
                num.pop(0)
                time.sleep(2)





                share|improve this answer




























                  4












                  4








                  4







                  Another solution is using list.pop(), as follows:



                  import time
                  num=[1,2,3,4]
                  while num:
                  num.pop(0)
                  time.sleep(2)





                  share|improve this answer















                  Another solution is using list.pop(), as follows:



                  import time
                  num=[1,2,3,4]
                  while num:
                  num.pop(0)
                  time.sleep(2)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 27 '18 at 16:17

























                  answered Dec 27 '18 at 15:48









                  SseinSsein

                  1,0141921




                  1,0141921























                      3














                      This does so, until eventually it runs out of elements to remove.



                      import time

                      num = [1, 2, 3, 4]
                      while num:
                      time.sleep(2)
                      del num[0]
                      print (num)





                      share|improve this answer





















                      • 7





                        Might be tempted to make that while num instead of while True - that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.

                        – Jon Clements
                        Dec 27 '18 at 15:41













                      • Good idea. Done

                        – Joshua Fox
                        Dec 27 '18 at 19:19
















                      3














                      This does so, until eventually it runs out of elements to remove.



                      import time

                      num = [1, 2, 3, 4]
                      while num:
                      time.sleep(2)
                      del num[0]
                      print (num)





                      share|improve this answer





















                      • 7





                        Might be tempted to make that while num instead of while True - that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.

                        – Jon Clements
                        Dec 27 '18 at 15:41













                      • Good idea. Done

                        – Joshua Fox
                        Dec 27 '18 at 19:19














                      3












                      3








                      3







                      This does so, until eventually it runs out of elements to remove.



                      import time

                      num = [1, 2, 3, 4]
                      while num:
                      time.sleep(2)
                      del num[0]
                      print (num)





                      share|improve this answer















                      This does so, until eventually it runs out of elements to remove.



                      import time

                      num = [1, 2, 3, 4]
                      while num:
                      time.sleep(2)
                      del num[0]
                      print (num)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 27 '18 at 19:19

























                      answered Dec 27 '18 at 15:38









                      Joshua FoxJoshua Fox

                      7,92495178




                      7,92495178








                      • 7





                        Might be tempted to make that while num instead of while True - that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.

                        – Jon Clements
                        Dec 27 '18 at 15:41













                      • Good idea. Done

                        – Joshua Fox
                        Dec 27 '18 at 19:19














                      • 7





                        Might be tempted to make that while num instead of while True - that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.

                        – Jon Clements
                        Dec 27 '18 at 15:41













                      • Good idea. Done

                        – Joshua Fox
                        Dec 27 '18 at 19:19








                      7




                      7





                      Might be tempted to make that while num instead of while True - that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.

                      – Jon Clements
                      Dec 27 '18 at 15:41







                      Might be tempted to make that while num instead of while True - that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.

                      – Jon Clements
                      Dec 27 '18 at 15:41















                      Good idea. Done

                      – Joshua Fox
                      Dec 27 '18 at 19:19





                      Good idea. Done

                      – Joshua Fox
                      Dec 27 '18 at 19:19











                      0














                      Try this code !



                      Use sleep function by importing time file & del command to remove the element from list.



                      Code :



                      import time

                      num = [1,2,3,4]
                      for i in num:
                      time.sleep(2)
                      print("Delete item ", i)
                      del i


                      Output :



                      Delete item  1                                                                                                          
                      Delete item 2
                      Delete item 3
                      Delete item 4





                      share|improve this answer




























                        0














                        Try this code !



                        Use sleep function by importing time file & del command to remove the element from list.



                        Code :



                        import time

                        num = [1,2,3,4]
                        for i in num:
                        time.sleep(2)
                        print("Delete item ", i)
                        del i


                        Output :



                        Delete item  1                                                                                                          
                        Delete item 2
                        Delete item 3
                        Delete item 4





                        share|improve this answer


























                          0












                          0








                          0







                          Try this code !



                          Use sleep function by importing time file & del command to remove the element from list.



                          Code :



                          import time

                          num = [1,2,3,4]
                          for i in num:
                          time.sleep(2)
                          print("Delete item ", i)
                          del i


                          Output :



                          Delete item  1                                                                                                          
                          Delete item 2
                          Delete item 3
                          Delete item 4





                          share|improve this answer













                          Try this code !



                          Use sleep function by importing time file & del command to remove the element from list.



                          Code :



                          import time

                          num = [1,2,3,4]
                          for i in num:
                          time.sleep(2)
                          print("Delete item ", i)
                          del i


                          Output :



                          Delete item  1                                                                                                          
                          Delete item 2
                          Delete item 3
                          Delete item 4






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 27 '18 at 16:02









                          UsmanUsman

                          754614




                          754614






























                              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%2f53947391%2fhow-to-delete-list-item-automatically-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

                              Plaza Victoria

                              Puebla de Zaragoza

                              Musa