Don’t Run Loops in Python, Instead, Use These!

5 min read
Share on:

As Data science practitioners we always deal with large datasets and often we need to modify one or multiple columns. Using a loop for that kind of task is slow.

In this blog, I will take you through a few alternative approaches which are faster than loops in python.

1. Filter

Based on the name we can easily guess what it does. It filters iterable objects for us. We are going to pass the filtering conditions in form of a function and this function is going to use to filter each element in the iterable object.


filter(function, iterable)

Now let’s compare the python filter’s performance compare to for loop and while loop.

Here, I’m creating a list of 100000 sequential items and then will do a factorial of even numbers. In the end, we will sum these factorial values.

2 thoughts on “Don’t Run Loops in Python, Instead, Use These!”

Comments are closed.

AnupTechTips We would like to show you notifications for the latest news and updates.
Allow Notifications