def nlargest(n, iterable, key=None):
<EOL>if n == <NUM_LIT:1>:<EOL><INDENT>it = iter(iterable)<EOL>head = list(islice(it, <NUM_LIT:1>))<EOL>if not head:<EOL><INDENT>return []<EOL><DEDENT>if key is None:<EOL><INDENT>return [max(chain(head, it))]<EOL><DEDENT>return [max(chain(head, it), key=key)]<EOL><DEDENT>try:<EOL><INDENT>size = len(iterable)<EOL><DEDENT>except (TypeError, AttributeError):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if n >= size:<EOL><INDENT>return sorted(iterable, key=key, reverse=True)[:n]<EOL><DEDENT><DEDENT>if key is None:<EOL><INDENT>it = izip(iterable, count(<NUM_LIT:0>,-<NUM_LIT:1>))                    <EOL>result = _nlargest(n, it)<EOL>return map(itemgetter(<NUM_LIT:0>), result)                   <EOL><DEDENT>in1, in2 = tee(iterable)<EOL>it = izip(imap(key, in1), count(<NUM_LIT:0>,-<NUM_LIT:1>), in2)             <EOL>result = _nlargest(n, it)<EOL>return map(itemgetter(<NUM_LIT:2>), result)                       <EOL>
Find the n largest elements in a dataset.

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