+ 2

What are lambda functions used for in Python?

I was recently trying to complete the Average Word Length code coach challenge when a friend of mine suggested I used this code: sentence = input() filtered = ''.join(filter(lambda x: x not in '".,;!-', sentence)) words = [word for word in filtered.split() if word] avg = sum(map(len, words))/len(words) print(avg) Since I’m new at Python, and I don’t know anything about lambda functions, it got me wondering what they’re are used for. I hope I can get some more insight about them with your help. Solo Learn’s code testing utility ended up rejecting the code so don’t even bother using it to complete the challenge.

20th May 2020, 11:54 PM
Cinnamon-One
Cinnamon-One - avatar
5 Réponses
+ 5
Hey CinnamonOne, Lambda functions are simply functions that do not exceed a single line (but can be used for complex functionality too). For example, consider the below code for getting a square of a number ______________________________ Code using normal function: def square(number): return number * number ____________________________ Code using Lambda function: square = lambda number: number * number _____________________________ Where number is used as a variable here. As you can see here the code becomes more readable and reduces the number of lines.
27th May 2020, 8:10 AM
Solo L earner
Solo L earner - avatar
+ 1
Lambda functions are also called anonymous functions as they don't require a name. They fulfil the functionality of a normal function but are quiker, shorter and easier to write. They are great for mapping list and filtering lists but also just great to write a quick disposable function in one line. Here you can learn more.. https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/anonymous-function
21st May 2020, 8:28 AM
Marcus born
Marcus born - avatar
+ 1
To make one liner functions Makes your code look smaller Hence, it is used for making codes of good designs Moreover, lambda is used for creating anonymous functions that are used extensively with 'map' / 'filter'
27th May 2020, 7:19 AM
Namit Jain
Namit Jain - avatar