def nsmallest(n, iterable, key=None):
    """Find the n smallest elements in a dataset.

    Equivalent to:  sorted(iterable, key=key)[:n]
    """
    if key is None:
        return _nsmallest(n, iterable)
    in1, in2 = tee(iterable)
    it = izip(imap(key, in1), count(), in2)                 # decorate
    result = _nsmallest(n, it)
    return map(itemgetter(2), result)                       # undecorate
Find the n smallest elements in a dataset.

    Equivalent to:  sorted(iterable, key=key)[:n]
    
recipes/Python/355690_Add_key_argument_mmax_heapqnsmallest/recipe-355690.py