From 391ba51a35ad3d7f1d43ff286b812a36ecb999da Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 21 Feb 2009 23:27:02 +0000 Subject: [PATCH] Backport 69858: In Py3.x, a list comprehension is now faster than list(map(itemgetter(0), iterable)). --- Lib/heapq.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/heapq.py b/Lib/heapq.py index 55a3027eb6e2..638ecd17a299 100644 --- a/Lib/heapq.py +++ b/Lib/heapq.py @@ -130,7 +130,7 @@ __all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge', 'nlargest', 'nsmallest', 'heappushpop'] from itertools import islice, repeat, count, tee -from operator import itemgetter, neg +from operator import neg import bisect def heappush(heap, item): @@ -357,11 +357,11 @@ def nsmallest(n, iterable, key=None): if key is None: it = zip(iterable, count()) # decorate result = _nsmallest(n, it) - return list(map(itemgetter(0), result)) # undecorate + return [r[0] for r in result] # undecorate in1, in2 = tee(iterable) it = zip(map(key, in1), count(), in2) # decorate result = _nsmallest(n, it) - return list(map(itemgetter(2), result)) # undecorate + return [r[2] for r in result] # undecorate _nlargest = nlargest def nlargest(n, iterable, key=None): @@ -372,11 +372,11 @@ def nlargest(n, iterable, key=None): if key is None: it = zip(iterable, map(neg, count())) # decorate result = _nlargest(n, it) - return list(map(itemgetter(0), result)) # undecorate + return [r[0] for r in result] # undecorate in1, in2 = tee(iterable) it = zip(map(key, in1), map(neg, count()), in2) # decorate result = _nlargest(n, it) - return list(map(itemgetter(2), result)) # undecorate + return [r[2] for r in result] # undecorate if __name__ == "__main__": # Simple sanity test -- 2.47.3