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: itertools.permutations/.combinations should have len
Type: enhancement Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Claudiu Saftoiu, matrixise, r.david.murray, rhettinger, xiang.zhang
Priority: normal Keywords:

Created on 2016-08-04 07:25 by Claudiu Saftoiu, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg271952 - (view) Author: Claudiu Saftoiu (Claudiu Saftoiu) Date: 2016-08-04 07:25
Right now, itertools.permutations and itertools.combinations and itertools.product objects don't have a `len`. 

    >>> len(itertools.combinations(range(10), 5))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: object of type 'itertools.combinations' has no len()

I propose that a `len` be added to them, which computes (& caches) & returns the length. If the underlying iterator doesn't have a length, then they can raise a TypeError as they do now.
msg271953 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-08-04 07:39
You have to exhausts the iterator to get the actual length. Some iterators have __length_hint__ but it's not required to give the truth.
msg271954 - (view) Author: Claudiu Saftoiu (Claudiu Saftoiu) Date: 2016-08-04 07:54
Ahh, I see. In that case, if the object passed-in has a length, use that. If not, then raise TypeError. e.g. proposed behavior:

>>> len(itertool.combinations(range(10), 2))
45
>>> len(itertool.combinations(iter(range(10)), 2))
...
TypeError: itertools.combinations underlying object has no len()
msg271955 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2016-08-04 08:22
__length_hint__ returns an estimated length for the object.

As you said, if the object passed-in has a length, we could use it. If not, then raise TypeError. But we will have two different behaviors in function of the parameter.
msg271970 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-08-04 13:02
It may be time to create a faq entry about len and itertools; it seems to be a very popular idea. For various reasons (documented in several issues in this tracker now), we are not going to add len to the iterators in itertools.  See issue 24849, and in particular msg248496.
History
Date User Action Args
2022-04-11 14:58:34adminsetgithub: 71865
2016-08-04 13:02:21r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg271970

resolution: rejected
stage: resolved
2016-08-04 08:22:43matrixisesetnosy: + matrixise
messages: + msg271955
2016-08-04 07:54:58Claudiu Saftoiusetmessages: + msg271954
2016-08-04 07:41:46xiang.zhangsetnosy: + rhettinger
2016-08-04 07:39:34xiang.zhangsetnosy: + xiang.zhang
messages: + msg271953
2016-08-04 07:25:07Claudiu Saftoiucreate