Skip to content

group by does not sort data before grouping #134797

Not planned
@balakhaniyan

Description

@balakhaniyan

Bug report

Bug description:

itertools.groupby(dynamic_properties, lambda d: d.table_id)

When data is not sorted base on key, group by does not work correctly and it needs to be sorted...
Isn't it a bug?

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Activity

picnixz

picnixz commented on May 27, 2025

@picnixz
Member

I don't think groupby is meant to sort data (at least the docs don't guarantee it). For instance,
"".join([k for k, g in groupby('AAAABBBCCDAABBB')]) returns ABCDAB.

More precisely, the docs mention (emphasis mine):

The operation of groupby() is similar to the uniq filter in Unix. It generates a break or new group every time the value of the key function changes (which is why it is usually necessary to have sorted the data using the same key function). That behavior differs from SQL’s GROUP BY which aggregates common elements regardless of their input order

This is because sorting data before would exhaust iterators but also add an additional cost that may be superfluous in some situations.

added
pendingThe issue will be closed if no feedback is provided
on May 27, 2025
removed
type-bugAn unexpected behavior, bug, or error
pendingThe issue will be closed if no feedback is provided
on May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      group by does not sort data before grouping · Issue #134797 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant