A lambda is a kind of an anonymous function in Python that can take any number of arguments but consist of only one expression.
This is intended as a compact syntax for writing functions.
Lambda functions are essentially identical to the equivalent def function. They are a different syntax for the same result. Read What is the purpose of Lambda expressions?
Lambda is used along some other built-in functions such as Map, Filter, and Reduce functions.
Python lambdas are like salt. A pinch in your spam, ham, and eggs will enhance the flavors, but too much will spoil the dish. Read further: How to Use Python Lambda Functions
print("\n====================\n")
textEmotion = ['happy', 'sad ', ' fear', 'ANGRY', 'disgust']
# Regular Function Implementation
print("Regular Function Implementation")
def keyfunc(string):
return string.strip().lower()
result1=sorted(textEmotion, key=keyfunc)
print(result1)
print("\n====================\n")
# Lambda Implementation
print("Lambda Implementation")
result2=sorted(textEmotion, key=lambda string: string.strip().lower())
print(result2)
print("\n====================\n")
print("Conclusion:")
print("Different implementation but result with the same effect.")

0 Comments