• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Writing Multiple Lines Lambda Expression in Python

Writing Multiple Lines Lambda Expression in Python

February 9, 2022 Leave a Comment

In Python, lambda functions are typically one line functions. It is possible to write multiple line lambda functions with “\” after each line, however is not truly pythonic.

lambda_expression = lambda x: True if x > 0 \
                                                     else False

If you need more than 1 line for a function, it is better to define your own function.

def customFunction(x):
    if x > 0:
        return True
    else:
        return False

In Python, lambda expressions are very useful for creating anonymous functions which can be applied on variables or collections of objects.

When using lambda functions in Python, we need to understand that the lambda construct is limited to expressions only.

Therefore, to use lambda expressions in their intended form, we are limited to one line.

However, it is possible to define a lambda expression with multiple lines since we can always use “\” to go to the next line in our Python code.

Below is an example of a multiple line lambda expression in Python.

lambda_expression = lambda x: True if x > 0 \
                                                     else False

However, in this case, it would have been just as easy to write the lambda as:

lambda_expression = lambda x: True if x > 0 else False

If you have a lot of logic or conditionals to check in your code, our recommendation is to define your own function and then use that function.

Hopefully this article has helped you understand how to work with lambda expressions and why it might not be the best idea to use a multiple line lambda in Python.

Other Articles You'll Also Like:

  • 1.  Python tanh – Find Hyperbolic Tangent of Number Using math.tanh()
  • 2.  Using Python to Check If a Number is a Perfect Square
  • 3.  islower Python – Check if All Letters in String Are Lowercase
  • 4.  Union of Lists in Python
  • 5.  Selenium minimize_window() Function to Minimize Window in Python
  • 6.  How to Check if a String Contains Vowels in Python
  • 7.  Using Python to Replace Backslash in String
  • 8.  Remove Specific Word from String in Python
  • 9.  Python ljust Function – Left Justify String Variable
  • 10.  Adjusting Python Turtle Screen Size with screensize() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy