]> granicus.if.org Git - python/commitdiff
In Py3.x, a list comprehension is now faster than list(map(itemgetter(0), iterable)).
authorRaymond Hettinger <python@rcn.com>
Sat, 21 Feb 2009 23:20:57 +0000 (23:20 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 21 Feb 2009 23:20:57 +0000 (23:20 +0000)
Lib/heapq.py

index b3616312d9136c00bc597dc64ff131b70a38485b..19a457b10dd8a5c1db1686ae77e7de30b22adaa4 100644 (file)
@@ -130,7 +130,6 @@ __all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
            'nlargest', 'nsmallest', 'heappushpop']
 
 from itertools import islice, repeat, count, tee, chain
-from operator import itemgetter
 import bisect
 
 def heappush(heap, item):
@@ -377,13 +376,13 @@ 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
 
     # General case, slowest method
     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):
@@ -415,13 +414,13 @@ def nlargest(n, iterable, key=None):
     if key is None:
         it = zip(iterable, count(0,-1))                     # decorate
         result = _nlargest(n, it)
-        return list(map(itemgetter(0), result))             # undecorate
+        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 list(map(itemgetter(2), result))                 # undecorate
+    return [r[2] for r in result]                           # undecorate
 
 if __name__ == "__main__":
     # Simple sanity test