Getting a min birth year and max death year from a list of years

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite
1












A very simple problem here (I'm learning Python) - given a list of tuples which represent (year of birth, year of death) for a group of people, I want to find the minimum "year of birth" and the maximum "year of death". Here's how I did it:



def testmax():
years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)] # sample data

endyr = max([deathyr for (birthyr,deathyr) in years])
startyr = min([birthyr for (birthyr,deathyr) in years])
print(startyr, endyr) # correctly returns 1804 1997


As you can see, I use list comprehensions to go through the years list twice. Time complexity O(2N) = O(N).



I can improve this with a simple function that is longer but does it through one iteration of the years list — I simply track (min, max) as my return variables that I keep checking on every iteration to get the min and max years that I need, in one iteration. Time complexity = O(N). What I don't like here is writing longer code (perhaps in a test/interview where time is limited).



My question: is there a better/cleaner/more Pythonic way you would do this? Would a lambda function work here, if so how? Even if you still do it in 2 iterations, I'd appreciate it if you could demo/explain any variations of the code (perhaps using lambda) that you would have used.







share|improve this question



























    up vote
    2
    down vote

    favorite
    1












    A very simple problem here (I'm learning Python) - given a list of tuples which represent (year of birth, year of death) for a group of people, I want to find the minimum "year of birth" and the maximum "year of death". Here's how I did it:



    def testmax():
    years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)] # sample data

    endyr = max([deathyr for (birthyr,deathyr) in years])
    startyr = min([birthyr for (birthyr,deathyr) in years])
    print(startyr, endyr) # correctly returns 1804 1997


    As you can see, I use list comprehensions to go through the years list twice. Time complexity O(2N) = O(N).



    I can improve this with a simple function that is longer but does it through one iteration of the years list — I simply track (min, max) as my return variables that I keep checking on every iteration to get the min and max years that I need, in one iteration. Time complexity = O(N). What I don't like here is writing longer code (perhaps in a test/interview where time is limited).



    My question: is there a better/cleaner/more Pythonic way you would do this? Would a lambda function work here, if so how? Even if you still do it in 2 iterations, I'd appreciate it if you could demo/explain any variations of the code (perhaps using lambda) that you would have used.







    share|improve this question























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      A very simple problem here (I'm learning Python) - given a list of tuples which represent (year of birth, year of death) for a group of people, I want to find the minimum "year of birth" and the maximum "year of death". Here's how I did it:



      def testmax():
      years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)] # sample data

      endyr = max([deathyr for (birthyr,deathyr) in years])
      startyr = min([birthyr for (birthyr,deathyr) in years])
      print(startyr, endyr) # correctly returns 1804 1997


      As you can see, I use list comprehensions to go through the years list twice. Time complexity O(2N) = O(N).



      I can improve this with a simple function that is longer but does it through one iteration of the years list — I simply track (min, max) as my return variables that I keep checking on every iteration to get the min and max years that I need, in one iteration. Time complexity = O(N). What I don't like here is writing longer code (perhaps in a test/interview where time is limited).



      My question: is there a better/cleaner/more Pythonic way you would do this? Would a lambda function work here, if so how? Even if you still do it in 2 iterations, I'd appreciate it if you could demo/explain any variations of the code (perhaps using lambda) that you would have used.







      share|improve this question













      A very simple problem here (I'm learning Python) - given a list of tuples which represent (year of birth, year of death) for a group of people, I want to find the minimum "year of birth" and the maximum "year of death". Here's how I did it:



      def testmax():
      years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)] # sample data

      endyr = max([deathyr for (birthyr,deathyr) in years])
      startyr = min([birthyr for (birthyr,deathyr) in years])
      print(startyr, endyr) # correctly returns 1804 1997


      As you can see, I use list comprehensions to go through the years list twice. Time complexity O(2N) = O(N).



      I can improve this with a simple function that is longer but does it through one iteration of the years list — I simply track (min, max) as my return variables that I keep checking on every iteration to get the min and max years that I need, in one iteration. Time complexity = O(N). What I don't like here is writing longer code (perhaps in a test/interview where time is limited).



      My question: is there a better/cleaner/more Pythonic way you would do this? Would a lambda function work here, if so how? Even if you still do it in 2 iterations, I'd appreciate it if you could demo/explain any variations of the code (perhaps using lambda) that you would have used.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 3 at 19:02









      200_success

      123k14142399




      123k14142399









      asked Apr 3 at 18:58









      rishijd

      1585




      1585




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          What you really want to do is transpose the separate tuples into 2 tuples - one containing all the birth dates, and the other death dates.



          Fortunately, python's zip function can do this easily for you, using the *x syntax:



          years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)]
          births, deaths = zip(*years)
          # births = (1804, 1885, 1902, 1900)
          # deaths = (1884, 1997, 1975, 1989)
          print(min(births), max(deaths))
          # 1804 1997





          share|improve this answer





















          • Great! Thank you!
            – rishijd
            Apr 4 at 2:04










          • what's relative performance? I'm guessing slightly worse
            – Oscar Smith
            Apr 4 at 3:56










          • Its definitely better than iterating over years twice. Not the most memory efficient method though.
            – match
            Apr 4 at 6:02










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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%2fcodereview.stackexchange.com%2fquestions%2f191189%2fgetting-a-min-birth-year-and-max-death-year-from-a-list-of-years%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
          5
          down vote



          accepted










          What you really want to do is transpose the separate tuples into 2 tuples - one containing all the birth dates, and the other death dates.



          Fortunately, python's zip function can do this easily for you, using the *x syntax:



          years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)]
          births, deaths = zip(*years)
          # births = (1804, 1885, 1902, 1900)
          # deaths = (1884, 1997, 1975, 1989)
          print(min(births), max(deaths))
          # 1804 1997





          share|improve this answer





















          • Great! Thank you!
            – rishijd
            Apr 4 at 2:04










          • what's relative performance? I'm guessing slightly worse
            – Oscar Smith
            Apr 4 at 3:56










          • Its definitely better than iterating over years twice. Not the most memory efficient method though.
            – match
            Apr 4 at 6:02














          up vote
          5
          down vote



          accepted










          What you really want to do is transpose the separate tuples into 2 tuples - one containing all the birth dates, and the other death dates.



          Fortunately, python's zip function can do this easily for you, using the *x syntax:



          years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)]
          births, deaths = zip(*years)
          # births = (1804, 1885, 1902, 1900)
          # deaths = (1884, 1997, 1975, 1989)
          print(min(births), max(deaths))
          # 1804 1997





          share|improve this answer





















          • Great! Thank you!
            – rishijd
            Apr 4 at 2:04










          • what's relative performance? I'm guessing slightly worse
            – Oscar Smith
            Apr 4 at 3:56










          • Its definitely better than iterating over years twice. Not the most memory efficient method though.
            – match
            Apr 4 at 6:02












          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          What you really want to do is transpose the separate tuples into 2 tuples - one containing all the birth dates, and the other death dates.



          Fortunately, python's zip function can do this easily for you, using the *x syntax:



          years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)]
          births, deaths = zip(*years)
          # births = (1804, 1885, 1902, 1900)
          # deaths = (1884, 1997, 1975, 1989)
          print(min(births), max(deaths))
          # 1804 1997





          share|improve this answer













          What you really want to do is transpose the separate tuples into 2 tuples - one containing all the birth dates, and the other death dates.



          Fortunately, python's zip function can do this easily for you, using the *x syntax:



          years = [(1804, 1884), (1885,1997), (1902,1975), (1900,1989)]
          births, deaths = zip(*years)
          # births = (1804, 1885, 1902, 1900)
          # deaths = (1884, 1997, 1975, 1989)
          print(min(births), max(deaths))
          # 1804 1997






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Apr 3 at 19:26









          match

          4565




          4565











          • Great! Thank you!
            – rishijd
            Apr 4 at 2:04










          • what's relative performance? I'm guessing slightly worse
            – Oscar Smith
            Apr 4 at 3:56










          • Its definitely better than iterating over years twice. Not the most memory efficient method though.
            – match
            Apr 4 at 6:02
















          • Great! Thank you!
            – rishijd
            Apr 4 at 2:04










          • what's relative performance? I'm guessing slightly worse
            – Oscar Smith
            Apr 4 at 3:56










          • Its definitely better than iterating over years twice. Not the most memory efficient method though.
            – match
            Apr 4 at 6:02















          Great! Thank you!
          – rishijd
          Apr 4 at 2:04




          Great! Thank you!
          – rishijd
          Apr 4 at 2:04












          what's relative performance? I'm guessing slightly worse
          – Oscar Smith
          Apr 4 at 3:56




          what's relative performance? I'm guessing slightly worse
          – Oscar Smith
          Apr 4 at 3:56












          Its definitely better than iterating over years twice. Not the most memory efficient method though.
          – match
          Apr 4 at 6:02




          Its definitely better than iterating over years twice. Not the most memory efficient method though.
          – match
          Apr 4 at 6:02












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191189%2fgetting-a-min-birth-year-and-max-death-year-from-a-list-of-years%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?