Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorter alternative for 'lambda' keyword?

Tags:

Background:

Python is about simplicity and readable code. It has gotten better over the versions and I am a huge fan! However, typing l a m b d a every time I have to define a lambda is not fun (you may disagree). The problem is, these 6 characters l a m b d a make my statements longer, especially if I nest a couple of lambdas inside maps and filters. I am not nesting more than 2 or three, because it takes away the readability of python, even then typing l a m b d a feels too verbose.

The actual question (is in comments):

# How to rename/alias a keyword to a nicer one?  lines = map(lmd x: x.strip(), sys.stdin)  # OR, better yet, how to define my own operator like -> in python? lines = map(x -> x.strip(), sys.stdin) # Or may be :: operator is pythonic lines = map(x :: x.strip(), sys.stdin)  # INSTEAD of this ugly one. Taking out this is my goal! lines = map(lambda x: x.strip(), sys.stdin) 

I am happy to add import like this:

from myfuture import lmd_as_lambda # OR from myfuture import lambda_operator 
like image 698
Thamme Gowda Avatar asked Aug 19 '17 00:08

Thamme Gowda


People also ask

What can I use instead of lambda in Python?

One of good alternatives of lambda function is list comprehension. For map() , filter() and reduce() , all of them can be done using list comprehension. List comprehension is a solution between a regular for-loop and lambda function.

What is reduce lambda?

The reduce() function in Python takes in a function and a list as an argument. The function is called with a lambda function and an iterable and a new reduced result is returned. This performs a repetitive operation over the pairs of the iterable. The reduce() function belongs to the functools module.

Which keyword is used for lambda function?

What are lambda functions in Python? In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.

Is Lamda a keyword in Python?

The lambda keyword is used to create small anonymous functions. A lambda function can take any number of arguments, but can only have one expression. The expression is evaluated and the result is returned.


1 Answers

The good news is: You don't need to use map or filter at all, you can use generator expressions (lazy) or list comprehensions (eager) instead and thus avoid lambdas completely.

So instead of:

lines = map(lambda x: x.strip(), sys.stdin) 

Just use:

# You can use either of those in Python 2 and 3, but map has changed between # Python 2 and Python 3 so I'll present both equivalents: lines = (x.strip() for x in sys.stdin)  # generator expression (Python 3 map equivalent) lines = [x.strip() for x in sys.stdin]  # list comprehension   (Python 2 map equivalent) 

It's probably also faster if you use comprehensions. Very few functions are actually faster when used in map or filter - and using a lambda there is more of an anti-pattern (and slow).


The question only contained an example for map, but you can also replace filter. For example if you want to filter out odd numbers:

filter(lambda x: x%2==0, whatever) 

You can use a conditional comprehension instead:

(x for x in whatever if x%2==0) [x for x in whatever if x%2==0] 

You could even combine a map and filter in one comprehension:

(x*2 for x in whatever if x%2==0) 

Just consider how that would look like with map and filter:

map(lambda x: x*2, filter(lambda x: x%2==0, whatever)) 

Note: That doesn't mean lambda isn't useful! There are lots of places where lambdas are very handy. Consider the key argument for sorted (and likewise for min and max) or functools.reduce (but better keep away from that function, most of the times a normal for-loop is more readable) or itertools that require a predicate function: itertools.accumulate, itertools.dropwhile, itertools.groupby and itertools.takewhile. Just to name a few examples where a lambda could be useful, there are probably lots of other places as well.

like image 143
MSeifert Avatar answered Sep 20 '22 08:09

MSeifert