Challenge 3: A Generic last() Function — Possible Solution ==================================================================== from typing import TypeVar T = TypeVar("T") def last(items: list[T]) -> T: return items[-1] print(last([1, 2, 3])) print(last(["a", "b", "c"])) Output: 3 c WHY THIS WORKS AS AN ANSWER ------------------------------ T = TypeVar("T") reuses this chapter's own TypeVar setup exactly — a placeholder type name that stands in for "whatever type this particular call actually uses," not a fixed type of its own. def last(items: list[T]) -> T: mirrors the chapter's own first(items: list[T]) -> T function shape precisely, with one change: items[-1] instead of items[0], reusing negative indexing from Course 1, Chapter 5 to grab the LAST element of the list rather than the first — the exact modification the challenge asks for, with the generic typing mechanism completely unchanged. Calling last([1, 2, 3]) has mypy infer T = int for that specific call, so the declared return type resolves to int — and indeed, items[-1] returns 3, an int. Calling last(["a", "b", "c"]) separately has mypy infer T = str instead, and items[-1] correctly returns "c", a str. The SAME function definition handles both calls correctly without needing two separate versions (one for int, one for str) — reusing this chapter's own explanation that TypeVar is what lets one function work generically across different types, with the actual type resolved fresh for each individual call rather than fixed once at definition time.