(n, iterable, key=None)
return [r[2] for r in result]
Find the n largest elements in a dataset.

    Equivalent to:  sorted(iterable, key=key, reverse=True)[:n]
Find the n largest elements in a dataset.
Find the n largest elements in a dataset .
def nlargest(n, iterable, key=None):
    """Find the n largest elements in a dataset.

    Equivalent to:  sorted(iterable, key=key, reverse=True)[:n]
    """

    # Short-cut for n==1 is to use max() when len(iterable)>0
    if n == 1:
        it = iter(iterable)
        head = list(islice(it, 1))
        if not head:
            return []
        if key is None:
            return [max(chain(head, it))]
        return [max(chain(head, it), key=key)]

    # When n>=size, it's faster to use sorted()
    try:
        size = len(iterable)
    except (TypeError, AttributeError):
        pass
    else:
        if n >= size:
            return sorted(iterable, key=key, reverse=True)[:n]

    # When key is none, use simpler decoration
    if key is None:
        it = zip(iterable, count(0,-1))                     # decorate
        result = _nlargest(n, it)
        return [r[0] for r in result]                       # undecorate

    # General case, slowest method
    in1, in2 = tee(iterable)
    it = zip(map(key, in1), count(0,-1), in2)               # decorate
    result = _nlargest(n, it)
    return [r[2] for r in result]
def nlargest ( n , iterable , key = None ) : # Short-cut for n==1 is to use max() when len(iterable)>0 if n == 1 : it = iter ( iterable ) head = list ( islice ( it , 1 ) ) if not head : return [ ] if key is None : return [ max ( chain ( head , it ) ) ] return [ max ( chain ( head , it ) , key = key ) ] # When n>=size, it's faster to use sorted() try : size = len ( iterable ) except ( TypeError , AttributeError ) : pass else : if n >= size : return sorted ( iterable , key = key , reverse = True ) [ : n ] # When key is none, use simpler decoration if key is None : it = zip ( iterable , count ( 0 , - 1 ) ) # decorate result = _nlargest ( n , it ) return [ r [ 0 ] for r in result ] # undecorate # General case, slowest method in1 , in2 = tee ( iterable ) it = zip ( map ( key , in1 ) , count ( 0 , - 1 ) , in2 ) # decorate result = _nlargest ( n , it ) return [ r [ 2 ] for r in result ]