Kodewick
Start learning →
Starter←→0 / 80

Heaviest in the bag

#042StarterSortingLists3 min

This backpack is heavy! Each thing is noted as (name, grams). Write heaviest(items) to return the name of the heaviest thing. No two weigh the same; an empty bag gives None (already in the starter).

Examples
heaviest([("book", 800), ("water", 500), ("laptop", 1500)])→'laptop'
heaviest([])→None
main.py
def heaviest(items):
    if not items:
        return None
    # your code here: max with a key, by the grams (the 2nd part)
    top = items[0]
    return top[0]
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.