grahamboree,
@grahamboree@mastodon.gamedev.place avatar

Super "fun" bug: groupby in itertools ostensibly groups items by the result of a key function, however it just groups similar sequential items in the input list. You can't safely use it directly to build a dictionary. It will silently drop some of your items.

>>> data = ['aa', 'bbb', 'cc']
>>> grouped = {k: list(v) for k, v in groupby(data, key=len)}
>>> print(grouped)
{2: ['cc'], 3: ['bbb']}

emattiza,
@emattiza@hachyderm.io avatar

@grahamboree Its documented in the 2nd paragraph of the docs for it, but definitely a watch out for folks. https://docs.python.org/3/library/itertools.html#itertools.groupby

slott56,
@slott56@fosstodon.org avatar

@grahamboree the “requires sorted data” isn’t too prominent in the docs is it?

treyhunner,
@treyhunner@mastodon.social avatar

@slott56 @grahamboree I agree that this could be considered a documentation bug.

I remember misunderstanding the niche use of groupby years ago because the documentation is a bit subtle on the fact that it groups consecutive elements.

grahamboree,
@grahamboree@mastodon.gamedev.place avatar

@treyhunner @slott56 It's a function naming bug. "groupby" means something very specific that this function does not do.

Ask 100 engineers what this function does and 95 of them will give you the sql "group by" behavior answer, while the other 5 will be people who've been burned by this.

treyhunner,
@treyhunner@mastodon.social avatar

@grahamboree @slott56 I hear you.

I named a exercise "group_by" many years ago because it's an apt description of what it does even though itertools.groupby won't be of much help for solving it.

https://www.pythonmorsels.com/exercises/b8520efdf5b3442799f5b4a8a399c32d/

Unfortunately, groupby has been around since 2004, so we may have missed the boat on convincing core devs to rename it. 😬

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • kavyap
  • ngwrru68w68
  • osvaldo12
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • thenastyranch
  • Youngstown
  • khanakhh
  • everett
  • slotface
  • tacticalgear
  • rosin
  • cubers
  • megavids
  • normalnudes
  • modclub
  • ethstaker
  • InstantRegret
  • GTA5RPClips
  • cisconetworking
  • Durango
  • Leos
  • provamag3
  • tester
  • anitta
  • JUstTest
  • lostlight
  • All magazines