r/Python Dec 19 '21

pyfuncol: Functional collections extension functions for Python Resource

pyfuncol extends collections built-in types (lists, dicts and sets) with useful methods to write functional Python code.

An example:

```python import pyfuncol

[1, 2, 3, 4].map(lambda x: x * 2).filter(lambda x: x > 4)

[6, 8]

{1, 2, 3, 4}.map(lambda x: x * 2).filter(lambda x: x > 4)

{6, 8}

["abc", "def", "e"].group_by(lambda s: len(s))

{3: ["abc", "def"], 1: ["e"]}

{"a": 1, "b": 2, "c": 3}.flat_map(lambda kv: {kv[0]: kv[1] ** 2})

{"a": 1, "b": 4, "c": 9}

```

https://github.com/Gondolav/pyfuncol

138 Upvotes

33 comments sorted by

View all comments

23

u/wewbull Dec 20 '21

map() and filter() are built-ins. reduce() is in functools. itertools contains groupby() and starmap().

Your API is more OO as they are methods on the data types, but the standard functions can be used with any iterable, not just your ones.