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.

classification
Title: name of 2nd parameter to itertools.groupby()
Type: enhancement Stage:
Components: Documentation Versions: Python 3.3, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, rhettinger, ukl
Priority: normal Keywords: patch

Created on 2014-06-15 14:07 by ukl, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
groupby-keyfunc.patch ukl, 2014-06-15 14:07 review
Messages (2)
msg220639 - (view) Author: Uwe Kleine-König (ukl) * Date: 2014-06-15 14:07
The name of the 2nd parameter to itertools.groupby() is documented inconsitently. Sometimes it's "key", sometimes "keyfunc". The code actually uses "key", so I adapted all occurences I found to "key".

>>> from itertools import groupby
>>> groupby.__doc__
'groupby(iterable[, keyfunc]) -> create an iterator which returns\n(key, sub-iterator) grouped by each value of key(value).\n'

>>> groupby([], keyfunc=lambda x: x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'keyfunc' is an invalid keyword argument for this function

>>> groupby([], key=lambda x: x)
<itertools.groupby object at 0x7fee025d2048>
msg220674 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-06-15 21:26
There is a bit an inconsistency but it is more helpful than harmful most of the time.  The glossary defines a key-function which is used in a number of places such such as sorted(), min(), nsmallest() and others.  In all those cases, the parameter for the key-function is *key*.

It might feel "more consistent" to call it "key" everywhere, but that leaves out the explanation that *key* represents a key-function.

The other issue is that groupby() returns a (key, sub-iterator) pair where "key" is the result of key-function, not the key itself.

What we have now is imperfect but it does a reasonably good job helping people understand what the function does.  IMO, changing it to be "key" would make the docs less intelligible.

Thank you for the patch, but I'm going to pass on it.
History
Date User Action Args
2022-04-11 14:58:04adminsetgithub: 65970
2014-06-15 21:26:02rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg220674
2014-06-15 20:38:59ned.deilysetnosy: + rhettinger
2014-06-15 14:07:16uklcreate