0

I studied that lamda function can be used immediately and discarded where it is needed. What is the relationship between map, filter and lamda?

wins = map(lambda x : 'img' + str(x), range(5))
wins = list(wins)
Jay
  • 19
  • 1
  • It's not a lambda function, it's a lambda *expression*, which is just another way to define a function without having to bind it to a name. – chepner Nov 18 '19 at 15:56
  • 1
    There is no relation to `map` or `filter`, aside from the fact that a lambda expression can produce a value suitable for the first argument to either `map` or `filter`. – chepner Nov 18 '19 at 15:57
  • 3
    Possible duplicate of [Why are Python lambdas useful?](https://stackoverflow.com/questions/890128/why-are-python-lambdas-useful) – pault Nov 18 '19 at 15:57
  • 2
    lambda is for making "throwaway" functions, that you only really need to use once perhaps, and don't care for afterwards. That's it. map and filter accept "functions" as one of the arguments. That's essentially the extent of their relation to lambda, there's no real reason why map or filter must use lambdas in particular. You use a lambda if it makes sense, otherwise you use a traditional function. – Paritosh Singh Nov 18 '19 at 15:58
  • 1
    I'd also challenge the 'usually used with map', I never use map/filter, but use plenty of lamdas – Sayse Nov 18 '19 at 16:07
  • @chepner what is a lambda function then? – Chris_Rands Nov 18 '19 at 16:08
  • 1
    @Chris_Rands There's no such thing, aside from being a term used by people who think that lambda expressions and `def` statements produce fundamentally different objects. – chepner Nov 18 '19 at 16:10

1 Answers1

1

lambda is often used when there is a better/more efficient solution. While there are places to use lambda this isn't the most efficient or pythonic solution.

I'd suggest a list comprehension here as it's more readable and efficient for what you're doing.

>>> wins = [f'img{n}' for n in range(5)]
>>> wins
['img0', 'img1', 'img2', 'img3', 'img4']

If your version of python doesn't support f-strings use str.format

wins = ['img{}'.format(n) for n in range(5)]

Or if you want to use map then str.format works here too:

wins = list(map('img{}'.format, range(5)))
Jab
  • 26,853
  • 21
  • 75
  • 114
  • 1
    disagree, there are lots of valid uses for `lambda`, e.g. as a key function for `sorted()` or `itertools.groupby()` etc. – Chris_Rands Nov 18 '19 at 16:15
  • I didn't say it doesn't have it's uses but here it's absolutely not necessary. I was referring to how many times it's used when there are better options. – Jab Nov 18 '19 at 16:17
  • 2
    Replace "usually" with "often", and I'll agree with this a lot more. `lambda` expressions are overused, whether by pairing it with `map` when a list comprehension would be preferable, or when used to add a layer of indirection around something that could be used more efficiently by itself (e.g., `lambda x: int(x)` instead of simply `int`). – chepner Nov 18 '19 at 16:17
  • My apologies, I must have worded it wrong. Often is a better word @chepner! Thanks, that was my initial intent – Jab Nov 18 '19 at 16:18