Professional Documents
Culture Documents
Replacement of Loops
Replacement of Loops
Loops are expensive (slow) in Python and if you are working with a large number of rows then it’s
a crime.
I will take you through the theory and practical examples of the functions that you can use as an
alternative to loops in python.
1. Map
2. Filter
3. Reduce
Before starting with the above functions, let’s have a quick look at the lambda function if you are
not already familiar with it.
The Lambda function is an alternative to the regular functions. It can be defined in a single line of
code and thus takes less time and space in our code. For example, in the below code, we can see
the lambda function in action.
Regular Function
def multiply_by_2(x):
x*2
Lambda function
lambda x: x*2
1. Map
Using the map function, we can apply a function to each value of an iterable object (list, tuple,
etc. ).
Syntax :
Map(function, iterable)
Example:
Suppose we want to get a square of numbers in a list(iterable object).
We will first create a square() function for finding the square of a number.
def square(x):
return x*x
Then, we will use the map function to apply square() function to a list of input numbers.
input_list = [2, 3, 4, 5, 6] # Without lambda
result = map(square, input_list) # Using lambda function
result = map(lambda x: x*x, input_list) # converting the numbers into a list
list(result) # Output: [4, 9, 16, 25, 36]
def square(x):
return x*x
Then, we will use the map function to apply square() function to a list of input numbers.
input_list = [2, 3, 4, 5, 6] # Without lambda
result = map(square, input_list) # Using lambda function
result = map(lambda x: x*x, input_list) # converting the numbers into a list
list(result) # Output: [4, 9, 16, 25, 36]
2. Filter
Intuitively, the filter function is used to filter out values from an iterable object (list, tuple, sets,
etc.). The filtering conditions are set inside a function which is passed as an argument to the filter
function.
Syntax
filter(function, iterable)
Example:
We will use the filter function to filter for values less than 10.
def less_than_10(x):
if x < 10:
return x
Then, we will use the filter function to apply less_than_10() function on the list of values.
input_list = [2, 3, 4, 5, 10, 12, 14] # Without lambda
list(filter(less_than_10, input_list)) # using lambda function
list(filter(lambda x: x < 10, input_list)) # Output: [2, 3, 4, 5]
3. Reduce
Reduce function is a bit different from the map and filter functions. It is applied iteratively to all
the values of the iterable object and returns only one value.
In the example below, a list of numbers is reduced by applying the addition function. The final
output will sum of all the numbers of the list i.e. 15.
Syntax
reduce(func, iterable)
Example:
Next, in order to get the sum of all the numbers in a list, we will apply this addition function as an
argument to the reduce function.
from functools import reduceinput_list = [1, 2, 3, 4, 5] # Without Lambda function
reduce(addition, input_list)) # With Lambda function
reduce(lambda x,y: x+y, input_list)) # Output: 15