Title: itertools.count() confusingly mentions zip() and sequence numbers
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: docs@python, rhettinger, serhiy.storchaka, terry.reedy, trey
Priority: low Keywords:

Created on 2018-03-11 18:16 by trey, last changed 2018-03-16 22:01 by rhettinger. This issue is now closed.

Messages (5)
msg313606 - (view) Author: Trey Hunner (trey) * Date: 2018-03-11 18:16
From the itertools documentation:

> Also, used with zip() to add sequence numbers.

I'm not certain what the goal of the original sentence was, but I think it's unclear as currently written.

I assume this is what's meant:

my_sequence = [1, 2, 3, 4]
for i, item in zip(count(1), my_sequence):
    print(i, item)

This is a strange thing to note though because enumerate would be a better use here.

my_sequence = [1, 2, 3, 4]
for i, item in enumerate(my_sequence, start=1):
    print(i, item)

Maybe what is meant is that count can be used with a step while enumerate cannot?

my_sequence = [1, 2, 3, 4]
for i, item in zip(count(step=5), my_sequence):
    print(i, item)

If that's the case it seems like step should instead be mentioned there instead of "sequence numbers".
msg313612 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-03-11 18:37
For example zip(my_sequence, count(1)).
msg313633 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-03-12 05:07
> This is a strange thing to note though because enumerate 
> would be a better use here.

IIRC, the wording predates the addition of enumerate() and before enumerate() grew a *start* argument.  That said, enumerate() just addresses care the most common case.  It is still worth mentioning that count() is still useful for the general case of adding sequence numbers to data streams woven together by zip() -- like the way auto-increment is used in SQL:

    from time import ctime

    def timestamp():
        while True:
            yield ctime()
    def user_request():
        while True:
            yield input()
    logged_requests = zip(user_request(), count(1), timestamp(), cycle(available_servers))

> it seems like step should instead be mentioned there
> instead of "sequence numbers".

The *step* argument was a late addition to the API and isn't used much in practice.  When it is used, its meaning and use case tend to be self-evident (i.e. counting 60 seconds at a time, or counting backwards), so it doesn't warrant further elaboration.

The sentence as-is is imperfect (it makes you wonder why not just use enumerate) but it seems better than either saying less by not mentioning the use case or getting too wordy which would place too much emphasis on use cases less common that those served by enumerate().  So, my preference is to leave the sentence as it stands.  The intent of the two sentences mentioning map() and zip() was to hint at the possibilities while still keeping the paragraph primary focused on what count() actually does.
msg313975 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-03-16 21:55
I think 'sequential numbers' would be slightly better than 'sequence numbers'.  To be, it better includes stepped and descending sequences.  (Enough better that I would be willing to do the PR and merge.)  Otherwise, I think leave it alone.
msg313976 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-03-16 22:01
Since I don't like "sequential numbers", let's opt for the "leave it alone" option :-)
Date User Action Args
2018-03-16 22:01:24rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg313976

stage: resolved
2018-03-16 21:55:23terry.reedysetnosy: + terry.reedy

messages: + msg313975
versions: - Python 3.4, Python 3.5
2018-03-12 05:07:35rhettingersetpriority: normal -> low
assignee: docs@python -> rhettinger
messages: + msg313633
2018-03-11 18:37:58serhiy.storchakasetnosy: + rhettinger, serhiy.storchaka
messages: + msg313612
2018-03-11 18:16:31treycreate