Technical skill practice
Python max(): Select a Record with key, Ties and Empty Input
Python max returns the largest item under the comparison you specify. With key, it can compare a property while returning the original record. A default can handle an empty iterable. For equal comparison keys, the first encountered maximum wins in the checked example.
Start with a small, explicit task
Select a fictional proposal by score. Two proposals share the highest score, so state whether first input order or another property should break the tie. Compare the selected record with a separate calculation of the highest numeric score.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_max.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
{'name': 'Cedar', 'score': 11}
highest score: 11
empty: None
explicit name tie-break: Elm
proposals = [
{"name": "Pine", "score": 8},
{"name": "Cedar", "score": 11},
{"name": "Elm", "score": 11},
]
chosen = max(proposals, key=lambda item: item["score"])
highest = max(item["score"] for item in proposals)
empty = max([], key=lambda item: item["score"], default=None)
assert chosen is proposals[1]
assert chosen["name"] == "Cedar" and highest == 11
assert empty is None
alphabetical_tie = max(proposals, key=lambda item: (item["score"], item["name"]))
assert alphabetical_tie["name"] == "Elm"
print(chosen)
print("highest score:", highest)
print("empty:", empty)
print("explicit name tie-break:", alphabetical_tie["name"])
Explain the result
The score-only key selects Cedar because it appears before Elm among equal maxima. max returns that dictionary itself, confirmed by identity, rather than returning just the score. The generator expression computes the separate numeric maximum when that is all a caller needs.
The tuple key adds name as a secondary comparison, selecting Elm under the explicitly stated lexicographic rule. That is not a universal fair tie-break; it is simply a reproducible alternative. The empty call returns None because a default is supplied, allowing the caller to handle no proposal separately from a legitimate record.
Avoid these mistakes
Do not compare dictionaries directly and expect Python to infer a score field. Define a key that expresses the task. Also avoid sorting an entire collection solely to retrieve one maximum unless the sorted order is needed for another reason.
Questions, answered
Does key make max return the key value?
No. It chooses by that value but returns the original item; the chosen object is the Cedar record.
What should I do with empty input?
Choose an explicit default or handle the no-item error according to the interface. This example uses None.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.