On this page
  1. heapq.nsmallest
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

heapq.nsmallest

Prev: heapq.nlargest | Up: heapq
Rel: heapq.heapify, heapq.heapify_max, heapq.heappop, heapq.heappop_max
Ref: docs.python.org — heapq.nsmallest
Tags: #public #python3 #stdlib #heapq


Import

import heapq
from heapq import nsmallest

Signature & Overview

heapq.nsmallest(n, iterable, key=None)

Find the n smallest elements in a dataset.

Usage

import heapq

assert heapq.nsmallest(2, [5, 1, 9, 3, 7]) == [1, 3]

Gotchas

REPL Inspect

from heapq import nsmallest
help(nsmallest)

Linked from

Related notes