Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Jonathan Emerson
4,485 PointsPython Dictionary Help - finding the max with with multiple values
So say you have a dictionary of cars including the makes, models and year. Like below:
cars = {'Hyundai': ('Tucson', 2006), 'Mitsubishi': ('Outlander', 2015), 'Ford': ('Focus', 1998)}
I'm struggling when trying to loop over a dictionary when keys have more than a single value to return the key and values for the newest car ( here it would be the 'Mitsubishi': ('Outlander', 2015)).
I'm still very new to python, can anyone help explain how to approach these kind of problems? All the best,
1 Answer

Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsSo here's what I did:
cars = {'Hyundai': ('Tucson', 2006), 'Mitsubishi': ('Outlander', 2015), 'Ford': ('Focus', 1998)}
make = ""
year = 0
tup = ()
for key, value in cars.items():
if value[1] > year:
make = key
year = value[1]
tup = value
print(make)
print(tup)
So first off you need to loop over the dictionary and you need to compare the years of the cars to eachother. To do this, you create a temporary variable to hold the newest car at each iteration. Then you compare them to eachother, and if the year is greater than the previous year then you can replace the temporary year. At the end, you will have the newest car.
Jonathan Emerson
4,485 PointsJonathan Emerson
4,485 PointsAh, okay perfect, wouldn't believe how long that's been annoying me!
And the opposite to find the oldest would just mean changing the year to 2019 and flipping to <
Thank you!