[Python-checkins] r69858 - python/branches/py3k/Lib/heapq.py

raymond.hettinger python-checkins at python.org
Sun Feb 22 00:20:57 CET 2009


Author: raymond.hettinger
Date: Sun Feb 22 00:20:57 2009
New Revision: 69858

Log:
In Py3.x, a list comprehension is now faster than list(map(itemgetter(0), iterable)).



Modified:
   python/branches/py3k/Lib/heapq.py

Modified: python/branches/py3k/Lib/heapq.py
==============================================================================
--- python/branches/py3k/Lib/heapq.py	(original)
+++ python/branches/py3k/Lib/heapq.py	Sun Feb 22 00:20:57 2009
@@ -130,7 +130,6 @@
            '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 @@
     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 @@
     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


More information about the Python-checkins mailing list