Dev Helpers

Python / Algorithms and data

Find the three largest values

Dev Helpers · May 1, 2026

When a dashboard needs only a few leaders, a top-k operation avoids constructing a complete sorted report. Keep the desired output order explicit.

Worked example

import heapq
print(heapq.nlargest(3, [4, 9, 2, 7, 5]))

Result

[9, 7, 5]

Implementation notes

A ranking key and a tie-breaker serve different purposes. Include a stable secondary key if the same report must select exactly the same three entries.

Try it yourself

Request more items than the input contains and inspect ties.

Open the Python example →

Editorial date May 1, 2026 · Published December 6, 2026

Related articles

Run an async function

An async function call creates a coroutine. A top-level runner supplies the event loop that executes it.

Transpose a matrix

Transposing rows into columns assumes a decision about uneven rows. Ordinary zip stops at the shortest input.

Unpack with a remainder

Starred unpacking separates a fixed header from a variable-length tail. It still requires enough elements for the fixed positions.