0

I need some opinions about lambda function in python.

I have been using Lambda function in python. For example, DirList = [i for i in tmp if "PC" in i]. However today I heard that one lecturer said that lambda function is not required in python3.

I have been taught that if I use lambda properly, my code can be more efficient because rather than using statement(if, for, etc.), lambda is treated as an expression which causes less computational burden. But now I'm confused about whether I should use lambda.

3
  • 1
    "I have been taught that if I use lambda properly, my code can be more efficient because rather than using statement(if, for, etc.), lambda is treated as an expression which causes less computational burden." that is definitely not correct. Also, your example of a lambda DirList = [i for i in tmp if "PC" in i] doesn't use a lambda expression at all. In any case, yes, lambda expressions are not necessary, though sometimes people prefer them for convenience/readability. For what it's worth, Guido wanted to remove lambda from Python 3 Commented Mar 23, 2020 at 0:01
  • The example you give has nothing to do with "lambda function"s. If you were using a lambda, your code would have the actual word lambda in it. Because of this, it's not clear what you're talking about. Is the example wrong? Or are you really talking about list comprehensions? In which case, are you sure that you understood what the lecturer was talking about? Commented Mar 23, 2020 at 0:16
  • Again, if you are talking about list comprehensions, those are also not required. They are mildly faster, but that's isn't the primary reason to use them. Rather, they should be used to improve readability. As the amount of work per iteration increases then that advantage disappears anyway. Commented Mar 23, 2020 at 1:16

1 Answer 1

0

There should be no difference in the speed of both functions ref: lambda is slower than function call in python, why (spoiler alert, it's not)

A lambda is just a function created with a single expression and no name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.