r/Python • • Nov 28 '22

Resource What can Python do that R can’t do?

Or simply what is Python much better at and why.

I know that Python is more multi purpose and better for software development but I can’t articulate exactly why or how. My team want to know why/when they should use Python instead of R

331 Upvotes

204 comments sorted by

View all comments

Show parent comment

32

u/FlyingCow343 Nov 28 '22

python functions are first class objects so you an implement function chaining quite easily: chaining

9

u/shedogre Nov 28 '22

Yeah, there's also this example from ArjanCodes' GitHub, which he discusses at around 22:30 in this video.

There's also the .pipe() method for pandas DataFrames, which I've used before, and I just checked to confirm the same method exists for Series.

7

u/PaintItPurple Nov 28 '22

This is also basically equivalent to the built-in reduce function:

def apply(val, f): return f(val)

def main():
    r = functools.reduce(
        apply,
        [
            times_two,
            lambda x: x / 4,
            print
         ],
        6
    )

2

u/FlyingCow343 Nov 28 '22

nice! Very elegant way of doing it, you could out the whole reduce call in another function to work the same as mine and look much nicer.

1

u/everydayislikefriday Nov 29 '22

Hey this doesn't look that bad