Special functions in Python (Part 1)

sudhanshu sharma
May 23 · 4 min read

Lambda function

In Python we see constructs that might be unfamiliar to us. Python tries to make things easy to use and gives very powerful constructs in the form of special functions. One of those is Lambdas. These are anonymous functions, kind of use, and throw function. We don’t want to call them over and over again. Define, use them, and don’t bother to utilize them in the future.

A Lambda function i.e. a function with no name is always preceded by a name ‘lambda’ and after the keyword, we specify the input arguments for the function. For eg.

This lambda function takes 2 input arguments x and y. These functions don’t use the ‘def’ keyword as they are named function. We specify the 2 input parameters after the lambda keyword and then the body of the function starts. So, a lambda function takes arguments as x and y and a function on x to y will be returned and unlike any other function a return statement doesn’t need to be specified. In a more generic way, whatever calculation we perform in lambda, the result of that will be returned and could be assigned to a variable as illustrated in the image below.

We will take a working example and things would get more clearer.

addition_func = lambda x, y: x + y

Now, we will pass these 2 numbers which need to be added,

addition_func(30, 20)
OUTPUT 50

Filter function

They are typically used with lists if one has to apply the lambda function to all elements of a list.

Number_list = [31, 15, 13, 93, 7, 54, 92]

Now, if we want to filter all numbers which are greater than 50, we can use a lambda function to do this.

greater_than_30 = lambda x: x > 30

This will return TRUE if greater than 30, FALSE otherwise, and assign all these to greater_than_30 variable. In order to apply the above function to every element of the list, we use filter function. FIlter function contains the condition on which we want to filter the list and the list itself.

result = filter(greater_than_30, Number_list)

to print the output of the result, we would have to convert it into a list.

result = list(result)
[93, 54, 92]

As lists are of various types, this time we would take a list of strings. The objective is to find out the strings which are palindrome out of all names.

text = ["php", "w3r", "Python", "abcd", "Java", "aaa"]

Here, we have also used join function which is a string method and returns a string in which the elements of the sequence have been joined by an str separator.

map function

It generates a new list by applying a function to each element of the list.

Num_list =[1,2,3,4,5]

In another example, we will reverse each string in the list of strings.

Names_list= ['Sachin', 'Saurav' , 'Kohli', 'pip', 'Rohit', 'Bumrah']
ReversedList = list(map(lambda x : x[::-1], Names_list))
['nihcaS', 'varuaS', 'ilhoK', 'pip', 'tihoR', 'harmuB']

Reduce function

It again has got 2 input variables, x stores the aggregated function of all the values of the list so far, and y stores the current value of the iterator or the new element that we encounter in the list.

Eg. Let's do a sum of all elements of a list of numbers

Num_list = [13, 15, 213, 193, 524, 192]

In the next posts, we will try to cover other special functions like lists comprehension which would make the code cleaner and faster. Also, there will be a separate post on classes and inheritance concepts in python which would help you to code more efficiently and with lesser maintenance.

So, follow me for such useful posts in the future.

To read about my earlier posts, click below-

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

sudhanshu sharma

Written by

Lead data scientist at Accenture❤️ Stats, ML/AI, data, sports, avid reader

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

sudhanshu sharma

Written by

Lead data scientist at Accenture❤️ Stats, ML/AI, data, sports, avid reader

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store