Good for performing where multiple operation or condition check need to be performed
Good for performing short operation like arithmetic operation or condition check
Comments can be used to for easy readabilty
It reduce the readability of code
List Comprehension vs Lambda Function
We can achieve short syntax or one line of code from List Comprehension also. List Comprehension is a built-in function and can create and modify the list in fewer lines of code.
But the problem with the lambda function is, that it will return something, otherwise, it will give an error, if the value is not returned.
email=['[email protected]','[email protected]','[email protected]','[email protected]']
# Using list comprehension it will take 1 line to extract only alphabetic username
username2=[i.split('@') for i in email if i.split('@').isalpha()]
# Also using lambda function it will take 1 line to extract only alphabetic username
username3=list(map(lambda x: x.split('@') if(x.split('@').isalpha()) else "", email))
#It will give error, if value is not returned
username4=list(map(lambda x: x.split('@') if(x.split('@').isalpha()), email))
The reduce() function accepts two arguments in Python: a function and a list. It performs a repetitive operation over the pairs of the iterable and return a new reduced result. The reduce() function belongs to the functools module.
Here, 1st element is added to 2nd element.result of both will get added to 3rd element, and this goes on till the end of the list like (((('Hello ', + 'welcome ') + 'to ') + 'fresherbell ') + 'website ')
from functools import reduce
li = ["Hello ", "Welcome ", "to ", "fresherbell ", "website"]
str1 = reduce((lambda x, y: x + y), li)
Hello Welcome to fresherbell website
Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique
Don't hesitate to give us a call or send us a contact form message