5 ms·
I find myself reaching for reduce fairly often, still. What's the list-comprehension equivalent? map(fun, iter) --> [ fun(x) for x in iter ] filter(fu
by nickysielicki 3y ago
I find myself reaching for reduce fairly often, still. What's the list-comprehension equivalent?
map(fun, iter) --> [ fun(x) for x in iter ]
filter(fun, iter) --> [ x for x in iter if fun(x) ]
reduce(bifun, iter, initial) --> ???
edit: I'll also add that this is made worse by the fact that itertools lacks a sliding window function.
- az09mugen 3y agoLooks like it's been implemented since python 3.8 : https://stackoverflow.com/a/55890523 https://stackoverflow.com/a/55890523 With the walrus operator :=
- kstrauser 3y agoThere's not a great `reduce` replacement with comprehensions. It'd probably be something closer to: last = initial for value in iter: last = byfun(last, value) However, I don't find myself writing that frequently.
- versteegen 3y agoYou forgot the walrus operator! accum = initial series = [accum := bifun(accum, value) for value in seq] # result = series[-1] To be honest, this is the first time I've used the walrus operator.
- wenc 3y agoThis is very nice. Replaces reduce() for me. Now to upgrade to Python 3.8! (stock Python on my cloud machine is still 3.7)
- kstrauser 3y agoAll hail the walrus! (Seriously, I like that.)
- daureg 3y agodoesn't it use a lot of memory since we're only interested in the last value?
- FreakLegion 3y agoYou can do the same thing with a generator. x = 0 deque((x := x + 1 for _ in range(10)), maxlen=0) print(x) >> 10 Using deque to consume generators is an old trick documented in the itertools recipes: https://docs.python.org/3/library/itertools.html#itertools-recipes https://docs.python.org/3/library/itertools.html#itertools-r...
- selcuka 3y ago> this is made worse by the fact that itertools lacks a sliding window function We have `itertools.pairwise()` for making a sliding window of pairs, but yes, there is no function that accepts an arbitrary number of elements.
- deleted 3y ago[deleted]
- dr_kiszonka 3y agoIt is not in the standard library, but I quite like more-itertools: https://more-itertools.readthedocs.io/en/stable/api.html#windowing https://more-itertools.readthedocs.io/en/stable/api.html#win...
- nhumrich 3y agoI find it interesting that you use reduce so often. Most python functions that handle two inputs handle it generically by handling a list of inputs, so passing in a full list exploded as args is sufficient and reduce isn't needed. All of pythons built-ins at least work this way like sum(), print(), etc. As such, I have not ever reached for reduce. Sometimes a comprehension inside a function call though. What types of situations do you use reduce for?
- corethree 3y agoIt's used to replace all looping. A combination of map and reduce is an alternative to loops. Also you don't need map. You can use list comprehension to replace map. But list comprehensions can't replace reduce. Technically reduce can also replace map. The only primitive you need to do all your iterations is technically just reduce. If you want to get more elegant than you don't even need reduce. You can eliminate all redundant primitives and use functions themselves for iteration. It's Just recursion.
- laurent_du 3y agoIf you use reduce to replace looping, your coworkers probably hate you. Don't write Clojure in Python.
- corethree 3y agoIt's called functional programming. Coworkers don't hate it it's more readable in general to split between map and reduce and forego looping. It's less readable to use reduce for all looping even though technically you can.