0

I have a list of dictionaries that contain temperatures recorded at various times. I am looking to find the maximum temperature from all of the dictionaries in the list. Once the maximum temperature is found, I’d like to print both the maximum temperature and the time it was recorded at with the time and temperature values removed from the dictionary.

I’ve looked at other posts here and have been able to find the dictionary containing the maximum temperature and I have seen a very similar question at Python list of dicts, get max value index with a solution that's mentioned in the comments below where it does this but the entire dictionary is printed rather than just the temperature and time values.

I haven’t been able to get both the temperature and time values to print out. With the code below I have been able to get just the temperature printed out in the format I'd like it, but I haven't been able to get the time value to print with it.

results = [{'Time': '08:43', 'Temp': 67.0}, {'Time': '08:44', 'Temp': 66.0}, {'Time': '08:47', 'Temp': 70.0}, {'Time': '08:45', 'Temp': '67.0'}]

max_temp = max([value['Temp'] for value in results])

print (max_temp)

Output:

70.0

Desired Output:

70.0 08:47 (or similar)
  • 2
    Use `max` with a key: `max(results, key=lambda x: x.get('Temp'))` – cs95 Jan 14 '18 at 19:16
  • Thanks cᴏʟᴅsᴘᴇᴇᴅ. I've edited the question a bit to explain further on what I'm looking for in comparison to this solution but this is very close. – picodegallo Jan 14 '18 at 19:35
  • Still a duplicate. Just take a result and print `r.values()`. – cs95 Jan 14 '18 at 19:37

0 Answers0