Getting a min birth year and max death year from a list of years
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
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.
python python-3.x
add a comment |Â
up vote
2
down vote
favorite
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.
python python-3.x
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
python python-3.x
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.
python python-3.x
edited Apr 3 at 19:02
200_success
123k14142399
123k14142399
asked Apr 3 at 18:58
rishijd
1585
1585
add a comment |Â
add a comment |Â
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
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 overyears
twice. Not the most memory efficient method though.
â match
Apr 4 at 6:02
add a comment |Â
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
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 overyears
twice. Not the most memory efficient method though.
â match
Apr 4 at 6:02
add a comment |Â
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
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 overyears
twice. Not the most memory efficient method though.
â match
Apr 4 at 6:02
add a comment |Â
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
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
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 overyears
twice. Not the most memory efficient method though.
â match
Apr 4 at 6:02
add a comment |Â
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 overyears
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password