How to sort a list of time values?sorting based on time a dictionary when the time is h:mm format onlyregex for detecting subtitle errorsHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a flat list out of list of lists?Sorting an array of JavaScript objectsSort array of objects by string property valueHow to Sort a List<T> by a property in the objectHow to pair socks from a pile efficiently?

When were female captains banned from Starfleet?

Pre-mixing cryogenic fuels and using only one fuel tank

Shouldn’t conservatives embrace universal basic income?

Doesn't the system of the Supreme Court oppose justice?

Does "he squandered his car on drink" sound natural?

Quoting Keynes in a lecture

A variation to the phrase "hanging over my shoulders"

Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?

Why is the Sun approximated as a black body at ~ 5800 K?

Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?

How can ping know if my host is down

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

Can I turn my anal-retentiveness into a career?

What is the highest possible scrabble score for placing a single tile

Find the next value of this number series

How could a planet have erratic days?

Biological Blimps: Propulsion

What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?

The Digit Triangles

Why do ¬, ∀ and ∃ have the same precedence?

How to convince somebody that he is fit for something else, but not this job?

Why do Radio Buttons not fill the entire outer circle?

Is there any evidence that Cleopatra and Caesarion considered fleeing to India to escape the Romans?

Is this toilet slogan correct usage of the English language?



How to sort a list of time values?


sorting based on time a dictionary when the time is h:mm format onlyregex for detecting subtitle errorsHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a flat list out of list of lists?Sorting an array of JavaScript objectsSort array of objects by string property valueHow to Sort a List<T> by a property in the objectHow to pair socks from a pile efficiently?













7















I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?










share|improve this question




























    7















    I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?










    share|improve this question


























      7












      7








      7


      3






      I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?










      share|improve this question
















      I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?







      python list sorting time






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 0:51









      martineau

      69.3k1092186




      69.3k1092186










      asked Jul 18 '13 at 3:11









      intuitionintuition

      36115




      36115






















          4 Answers
          4






          active

          oldest

          votes


















          4














          import time

          sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)





          share|improve this answer






























            4














            Just sorted(time_list) works fine.



            >>> sorted(["14:10:01", "03:12:08"])
            ["03:12:08", "14:10:01"]





            share|improve this answer




















            • 11





              This works because the times are '0' padded.

              – SethMMorton
              Jul 18 '13 at 3:36


















            2














            sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


            Where each element in my_time_list is of the form you describe, for example:



            >>> my_time_list
            ["03:12:08", "14:10:01"]





            share|improve this answer

























            • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

              – hago
              Jul 18 '13 at 3:32












            • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

              – Owen
              Jul 18 '13 at 3:42



















            0














            you should be able use the method sort(key=str.lower) since your time is parsed as a string






            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%2f17713873%2fhow-to-sort-a-list-of-time-values%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









              4














              import time

              sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)





              share|improve this answer



























                4














                import time

                sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)





                share|improve this answer

























                  4












                  4








                  4







                  import time

                  sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)





                  share|improve this answer













                  import time

                  sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 18 '13 at 3:53









                  fabrizioMfabrizioM

                  31k126898




                  31k126898























                      4














                      Just sorted(time_list) works fine.



                      >>> sorted(["14:10:01", "03:12:08"])
                      ["03:12:08", "14:10:01"]





                      share|improve this answer




















                      • 11





                        This works because the times are '0' padded.

                        – SethMMorton
                        Jul 18 '13 at 3:36















                      4














                      Just sorted(time_list) works fine.



                      >>> sorted(["14:10:01", "03:12:08"])
                      ["03:12:08", "14:10:01"]





                      share|improve this answer




















                      • 11





                        This works because the times are '0' padded.

                        – SethMMorton
                        Jul 18 '13 at 3:36













                      4












                      4








                      4







                      Just sorted(time_list) works fine.



                      >>> sorted(["14:10:01", "03:12:08"])
                      ["03:12:08", "14:10:01"]





                      share|improve this answer















                      Just sorted(time_list) works fine.



                      >>> sorted(["14:10:01", "03:12:08"])
                      ["03:12:08", "14:10:01"]






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 23 '15 at 19:44









                      AndreL

                      3,13022034




                      3,13022034










                      answered Jul 18 '13 at 3:34









                      hagohago

                      1,17821215




                      1,17821215







                      • 11





                        This works because the times are '0' padded.

                        – SethMMorton
                        Jul 18 '13 at 3:36












                      • 11





                        This works because the times are '0' padded.

                        – SethMMorton
                        Jul 18 '13 at 3:36







                      11




                      11





                      This works because the times are '0' padded.

                      – SethMMorton
                      Jul 18 '13 at 3:36





                      This works because the times are '0' padded.

                      – SethMMorton
                      Jul 18 '13 at 3:36











                      2














                      sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


                      Where each element in my_time_list is of the form you describe, for example:



                      >>> my_time_list
                      ["03:12:08", "14:10:01"]





                      share|improve this answer

























                      • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                        – hago
                        Jul 18 '13 at 3:32












                      • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                        – Owen
                        Jul 18 '13 at 3:42
















                      2














                      sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


                      Where each element in my_time_list is of the form you describe, for example:



                      >>> my_time_list
                      ["03:12:08", "14:10:01"]





                      share|improve this answer

























                      • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                        – hago
                        Jul 18 '13 at 3:32












                      • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                        – Owen
                        Jul 18 '13 at 3:42














                      2












                      2








                      2







                      sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


                      Where each element in my_time_list is of the form you describe, for example:



                      >>> my_time_list
                      ["03:12:08", "14:10:01"]





                      share|improve this answer















                      sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


                      Where each element in my_time_list is of the form you describe, for example:



                      >>> my_time_list
                      ["03:12:08", "14:10:01"]






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 18 '13 at 3:43

























                      answered Jul 18 '13 at 3:19









                      OwenOwen

                      1,500714




                      1,500714












                      • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                        – hago
                        Jul 18 '13 at 3:32












                      • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                        – Owen
                        Jul 18 '13 at 3:42


















                      • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                        – hago
                        Jul 18 '13 at 3:32












                      • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                        – Owen
                        Jul 18 '13 at 3:42

















                      it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                      – hago
                      Jul 18 '13 at 3:32






                      it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                      – hago
                      Jul 18 '13 at 3:32














                      I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                      – Owen
                      Jul 18 '13 at 3:42






                      I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                      – Owen
                      Jul 18 '13 at 3:42












                      0














                      you should be able use the method sort(key=str.lower) since your time is parsed as a string






                      share|improve this answer



























                        0














                        you should be able use the method sort(key=str.lower) since your time is parsed as a string






                        share|improve this answer

























                          0












                          0








                          0







                          you should be able use the method sort(key=str.lower) since your time is parsed as a string






                          share|improve this answer













                          you should be able use the method sort(key=str.lower) since your time is parsed as a string







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 18 '13 at 3:25









                          ConcoConco

                          363




                          363



























                              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%2f17713873%2fhow-to-sort-a-list-of-time-values%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

                              Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

                              Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

                              How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page