Start learning free

LESSON 262 OF 673 · TUPLES, SORTING & BUILT-INS

Oldest Pet in Python: Finding a Maximum in Tuples

Practice Read the full explanation

You get a list of pets, pets. Each pet is a pair: its name and its age, like ("Rex", 4).

Return the name of the oldest pet.

Several pets may share the oldest age. Then return the one that comes first in pets.

The starter code shows how your language holds a pair.

 

Example 1:

Input: pets = [("Rex", 4), ("Bella", 7), ("Max", 2)]
Output: "Bella"
Explanation: Bella is 7.
Rex and Max are younger.

Example 2:

Input: pets = [("Luna", 3), ("Coco", 9), ("Milo", 9)]
Output: "Coco"
Explanation: Coco and Milo are both 9.
Coco comes first in the list.

Example 3:

Input: pets = [("Kiki", 0)]
Output: "Kiki"
Explanation: Kiki is the only pet.

 

Constraints:

  • pets has at least one pet.
  • Every age is a whole number from 0 to 30.
  • Each name is a capital letter followed by lowercase letters, like "Rex".
STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · TUPLES, SORTING & BUILT-INS

    Oldest Pet in Python, explained

    Problem overview

    The method oldestPet gets a list pets. Each pet is a tuple of a name and an age, like ("Rex", 4). It returns the name of the oldest pet. If several pets share the oldest age, the one that comes first in the list wins.

    For [("Rex", 4), ("Bella", 7), ("Max", 2)] the answer is "Bella". For [("Luna", 3), ("Coco", 9), ("Milo", 9)] it is "Coco", because Coco and Milo are both 9 and Coco comes first.

    Approach

    Keep the whole oldest pair seen so far in oldest, starting with pets[0]. Then check every pet: if it is strictly older than oldest, it takes over. Keeping the whole tuple, not just the age, means the name comes along for free.

    The comparison must be strict, >. A pet of the same age doesn't replace the one you have, so on a tie the first pet stays. With >= the last tied pet would win, and Milo would be returned in the second example.

    Step by step

    1. Set oldest = pets[0]. A pet is a tuple, so the age is at index 1 and the name at index 0.
    2. Loop over pets with pet.
    3. If pet[1] > oldest[1], this pet is older.
    4. Then set oldest = pet.
    5. After the loop, return oldest[0], the name only.

    The solution in Python

    class Solution:
        def oldestPet(self, pets: List[Tuple[str, int]]) -> str:
            oldest = pets[0]
            for pet in pets:
                if pet[1] > oldest[1]:
                    oldest = pet
            return oldest[0]
    

    Running it on the three examples returns Bella, Coco and Kiki (a list with one pet returns that pet's name).

    Tracing the tie example

    For [("Luna", 3), ("Coco", 9), ("Milo", 9)], this is oldest after each pass:

    petoldest after the pass
    ("Luna", 3)("Luna", 3)
    ("Coco", 9)("Coco", 9)
    ("Milo", 9)("Coco", 9)

    Milo is 9, but 9 is not greater than 9, so Coco stays. The loop's first pass compares the first pet with itself, which changes nothing.

    Another way, with a built-in

    Python's max accepts a key function: max(pets, key=lambda pet: pet[1])[0] returns Bella for the first example. When several items tie for the largest key, max returns the first of them, so on the tie example it also gives Coco. The loop does the same work by hand, and writing it once makes the "strictly greater" rule easy to see.

    Edge cases

    A list with one pet returns that pet's name, because the loop never finds a pet that is older. An age of 0 is a normal age here: [("Kiki", 0)] returns Kiki. If every pet has the same age, the first pet in the list is returned, which is exactly the tie rule. The statement guarantees at least one pet, so pets[0] is always safe.

    Why keep the whole pair

    You could track only the highest age in one variable and the matching name in another, updating both together. Keeping the tuple in one variable is simpler: one assignment, oldest = pet, moves name and age at the same time, so they can never get out of step.

    Common mistakes

    • Using >=, which breaks the tie rule.
    • Returning oldest instead of oldest[0]: the function returns a name, not a pair.
    • Mixing up the indexes: pet[0] is the name, pet[1] the age.
    • Starting oldest at 0 or at an empty value instead of the first pet. Ages can be 0, and the first pet is always a real candidate.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free