Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add group() to itertools #45984

Closed
KirkMcDonald mannequin opened this issue Dec 18, 2007 · 2 comments
Closed

Add group() to itertools #45984

KirkMcDonald mannequin opened this issue Dec 18, 2007 · 2 comments
Labels
extension-modules C modules in the Modules dir

Comments

@KirkMcDonald
Copy link
Mannequin

KirkMcDonald mannequin commented Dec 18, 2007

BPO 1643
Nosy @rhettinger
Files
  • itertools.group.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2007-12-18.07:14:14.987>
    created_at = <Date 2007-12-18.01:49:19.085>
    labels = ['extension-modules']
    title = 'Add group() to itertools'
    updated_at = <Date 2007-12-18.07:14:14.850>
    user = 'https://bugs.python.org/KirkMcDonald'

    bugs.python.org fields:

    activity = <Date 2007-12-18.07:14:14.850>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2007-12-18.07:14:14.987>
    closer = 'rhettinger'
    components = ['Extension Modules']
    creation = <Date 2007-12-18.01:49:19.085>
    creator = 'KirkMcDonald'
    dependencies = []
    files = ['8979']
    hgrepos = []
    issue_num = 1643
    keywords = []
    message_count = 2.0
    messages = ['58716', '58724']
    nosy_count = 2.0
    nosy_names = ['rhettinger', 'KirkMcDonald']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1643'
    versions = ['Python 2.6']

    @KirkMcDonald
    Copy link
    Mannequin Author

    KirkMcDonald mannequin commented Dec 18, 2007

    One question which is asked with surprising frequency in #python is how
    to yield multiple objects at a time from an iterable object. That is,
    given [1, 2, 3, 4, 5, 6], get [(1, 2), (3, 4), (5, 6)].

    The "grouper" function in the itertools recipes page provides one
    pattern for this. A similar function (which behaves differently when the
    length of the iterable is not evenly divisible by n) looks like this:

    def group(iterable, n=2):
        return itertools.izip(*(iter(iterable),)*n)

    This code is fairly opaque to the novice. It is ugly, and takes a bit of
    head-scratching to realize exactly what it is doing. Because this
    operation is asked for with some frequency, and because this general
    implementation is so ugly, I believe it belongs in the library.

    There is a related function which is asked for much less frequently, but
    which is a more general case of the group() function listed above. This
    other function has a third "step" argument. This argument controls how
    far each group is in advance of the previous group. For example:

    list(group([1, 2, 3, 4], n=2, step=1)) -> [(1, 2), (2, 3), (3, 4)]

    The original function is equivalent to this function when step is equal
    to n.

    Please find attached a patch with implementation, documentation, and tests.

    @KirkMcDonald KirkMcDonald mannequin added the stdlib Python modules in the Lib dir label Dec 18, 2007
    @rhettinger
    Copy link
    Contributor

    Sorry, I'm not interested in adding this to the module. Discussions to-
    date on the subject seem to show more interest in playing with grouper
    variants than in actual use cases. While the recipe given in the docs
    is somewhat opaque, it runs at C-speed (zero trips around the eval-
    loop) and it is encapsulated in a re-usable function. Writing this in C
    does nothing to improve the situation. Also, when people like to play
    with variants, there is no general agreement on useful requirements
    (like fill-in behavior or raising an exception on uneven length
    inputs). Trying to write option to meet all needs (n=2, step=1) makes
    the code more difficult to learn and use -- see several variants in
    Alex's Python Cookbook. Another issue is that we have to be very
    selective about adding tools to the module. Each addition makes the
    overall toolset harder to use -- it is better to have a good set of
    basic building blocks.

    @rhettinger rhettinger added extension-modules C modules in the Modules dir and removed stdlib Python modules in the Lib dir labels Dec 18, 2007
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant