• 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
  • Ruby
  • 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 max() function – Get Maximum of List or Dictionary with max() Function
  • 2.  Swap Two Values of Variables in Python
  • 3.  Convert Set to List in Python
  • 4.  Convert timedelta to Seconds in Python
  • 5.  How to Group By Columns and Find Mean in pandas DataFrame
  • 6.  Remove Every Nth Element from List in Python
  • 7.  Log Base 10 Python – Find Logarithm of Number with Base 10 with log10()
  • 8.  pandas unique – Get Unique Values in Column of DataFrame
  • 9.  Remove Leading and Trailing Characters from String with strip() in Python
  • 10.  Add Minutes to Datetime Variable Using Python timedelta() 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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy