(Replying to PARENT post)

The worst part of python is the lack of utility functions for munging collections. But it sits at a slightly higher level than this - things that are idiomatic in other scripting languages like groupBy(fn), sortBy(fn), countBy(fn), collate, are all inexplicably like IKEA self assembled furniture in Python instead of first class entities. It makes lots of routine data munging and algorithmic work much more annoying than it needs to be (comparing to Groovy, Ruby, etc).
πŸ‘€zmmmmmπŸ•‘4yπŸ”Ό0πŸ—¨οΈ0

(Replying to PARENT post)

Not sure what collate is supposed to be, but these idioms are hardly difficult if you're used to Python:

- groupBy is itertools.groupBy(lst, fn)

- sortBy is just lst.sort(key=fn)

- countBy is collections.Counter(map(fn, lst))

- Sibling comment mentioned flatten, which is just [item for sublist for sublist in lst]

More esoteric needs are usually met by itertools.

πŸ‘€smallnamespaceπŸ•‘4yπŸ”Ό0πŸ—¨οΈ0

(Replying to PARENT post)

It's a package, not a built in, but pytoolz [0] is a very complete solution to the type of functional programming munging you are taking about. I wish more people knew about it (no one here seems to have mentioned it). And it has been Cython optimized in another package called cytoolz [1]. The author explains in the Heritage section how it is an extension of and plays nice with itertools and functools [2].

For Python coders new to functional programming, and how it can make working with data easier, I highly recommend reading the following sections of the pytoolz docs: Composability, Function Purity, Laziness, and Control Flow [0].

[0] https://toolz.readthedocs.io/en/latest/

[1] https://github.com/pytoolz/cytoolz

[2] https://toolz.readthedocs.io/en/latest/heritage.html

πŸ‘€hcrispπŸ•‘4yπŸ”Ό0πŸ—¨οΈ0

(Replying to PARENT post)

Yes - the description of writing Python as like assembling Ikea furniture is absolutely spot-on. Yes, you can do it, and the results can be nice, but by God it is sometimes such a pain.

The comment showing where groipBy, sortBy etc can be found just shows the problem - they are all in different libraries.That's just plain annoying! And don't get me started on the pain of trying to build an Ordered Dictionary with a default initial value!

πŸ‘€steve_ghπŸ•‘4yπŸ”Ό0πŸ—¨οΈ0

(Replying to PARENT post)

> ...like groupBy(fn), sortBy(fn), countBy(fn), collate...

What leaps out at me is that these 3 functions are all straight out of relational algebra style worldview.

Python the language doesn't support relational algebra as a first class concept. The reason it feels like IKEA self assembly is probably because you are implicitly implementing a data model that isn't how Python thinks about collections.

πŸ‘€roenxiπŸ•‘4yπŸ”Ό0πŸ—¨οΈ0

(Replying to PARENT post)

It will take Python 10 more years to allow a flatten()...
πŸ‘€BurningFrogπŸ•‘4yπŸ”Ό0πŸ—¨οΈ0

(Replying to PARENT post)

I think the motivation there historically was to encourage use of generators and generator expressions β€” it’s definitely intentional, because things like `apply` and `reduce` used to be builtins but now they’re imports.

If you reach for itertools imports often in an interactive REPL, you might be interested in Pyflyby: https://labs.quansight.org/blog/2021/07/pyflyby-improving-ef...

πŸ‘€goodsideπŸ•‘4yπŸ”Ό0πŸ—¨οΈ0

(Replying to PARENT post)

Also many python std library collection methods modify their inputs.
πŸ‘€publicola1990πŸ•‘4yπŸ”Ό0πŸ—¨οΈ0

(Replying to PARENT post)

Some of those are given as examples in the functools / itertools doc pages.
πŸ‘€emmelaichπŸ•‘4yπŸ”Ό0πŸ—¨οΈ0

(Replying to PARENT post)

And it's simply amazing that functools doesn't fill this gap.
πŸ‘€gsinclairπŸ•‘4yπŸ”Ό0πŸ—¨οΈ0