This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add key argument to heapq functions
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: gpolo, rhettinger
Priority: low Keywords: patch

Created on 2008-01-22 10:54 by gpolo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit gpolo, 2008-01-22 11:09 Adds key argument to heapq functions (C version not patched)
Messages (3)
msg61496 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-01-22 10:54
Wouldn't it be useful to add a "key" argument to some heapq functions ?
So you could construct a heap from dict items list based on the second
item of each tuple, for example.
msg61497 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-01-22 11:09
Adding a simple patch that adds the "key" argument. To test this you
need to comment lines where it tries to import the C version of heapq.
msg64397 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-03-24 08:13
Rejecting the key-function patch in favor of the current typical 
approach of heapifying tuples that link a priority level with 
individual records.  I've been a heavy user of the module and one of 
its principal maintainers.  To date, I've seen very little need for key-
function argument.

Use cases aside, there is another design issue in that the key-function 
approach doesn't work well with the heap functions on regular lists.  
Successive calls to heap functions will of necessity call the key-
function multiple times for any given element.  This contrasts with sort
() where the whole purpose of the key function was to encapsulate the 
decorate-sort-undecorate pattern which was desirable because the key-
function called exactly once per element.
Date User Action Args
2022-04-11 14:56:30adminsetgithub: 46199
2013-04-27 08:17:14serhiy.storchakalinkissue4424 superseder
2008-03-24 08:13:40rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg64397
2008-01-22 15:37:36gvanrossumsetpriority: low
assignee: rhettinger
keywords: + patch
nosy: + rhettinger
2008-01-22 11:09:28gpolosetfiles: +
messages: + msg61497
2008-01-22 10:54:02gpolocreate