Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba86fa9

Browse files
committed
In Py3.x, a list comprehension is now faster than list(map(itemgetter(0), iterable)).
1 parent 912fbca commit ba86fa9

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

Lib/heapq.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@
130130
'nlargest', 'nsmallest', 'heappushpop']
131131

132132
from itertools import islice, repeat, count, tee, chain
133-
from operator import itemgetter
134133
import bisect
135134

136135
def heappush(heap, item):
@@ -377,13 +376,13 @@ def nsmallest(n, iterable, key=None):
377376
if key is None:
378377
it = zip(iterable, count()) # decorate
379378
result = _nsmallest(n, it)
380-
return list(map(itemgetter(0), result)) # undecorate
379+
return [r[0] for r in result] # undecorate
381380

382381
# General case, slowest method
383382
in1, in2 = tee(iterable)
384383
it = zip(map(key, in1), count(), in2) # decorate
385384
result = _nsmallest(n, it)
386-
return list(map(itemgetter(2), result)) # undecorate
385+
return [r[2] for r in result] # undecorate
387386

388387
_nlargest = nlargest
389388
def nlargest(n, iterable, key=None):
@@ -415,13 +414,13 @@ def nlargest(n, iterable, key=None):
415414
if key is None:
416415
it = zip(iterable, count(0,-1)) # decorate
417416
result = _nlargest(n, it)
418-
return list(map(itemgetter(0), result)) # undecorate
417+
return [r[0] for r in result] # undecorate
419418

420419
# General case, slowest method
421420
in1, in2 = tee(iterable)
422421
it = zip(map(key, in1), count(0,-1), in2) # decorate
423422
result = _nlargest(n, it)
424-
return list(map(itemgetter(2), result)) # undecorate
423+
return [r[2] for r in result] # undecorate
425424

426425
if __name__ == "__main__":
427426
# Simple sanity test

0 commit comments

Comments
 (0)