• 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 / Using if in Python Lambda Expression

Using if in Python Lambda Expression

February 24, 2022 Leave a Comment

In Python, we can use if statements to add conditions our lambda expressions. We can create if, elif, and else blocks in a Python lambda expression easily.

lambda_expression = lambda x: True if x > 0 else 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.

We can use if statements in Python lambda expressions easily.

Given an if condition, the syntax for using if in lambda expression is:

lambda <arguments> : <statement1> if <condition> else <statement2>

For example, let’s say you want to remove the last character of a string if the string starts with the letter ‘a’.

We can define a lambda expression with an if statement which checks if the string’s first character is ‘a’, and then returns the appropriate string.

Then, we will use map() to apply our lambda expression to every element in the list of strings.

Below is an example of using if with a lambda expression in Python.

list_of_strings = ["apple","banana","avocado","pear","lime","lemon","artichoke"]
list_after_lambda = list(map(lambda x: x[:-1] if x[0] == 'a' else x, list_of_strings))

print(list_after_lambda )

#Output:
['appl', 'banana', 'avocad', 'pear', 'lime', 'lemon', 'artichok']

As you can see, all of the strings which begin with ‘a’ have had their last letter removed.

Using if and else in a Multiple Condition Python Lambda Expression

You can define more complex expressions using multiple cases in a Python lambda function which is identical to if, elif and else in a regular function.

Let’s say we want to check a number of conditions in a list of numbers. We can define a multiple condition lambda function a structure similar to if, elif, and else.

To define a multiple condition lambda function, we can use a similar structure as above, where the value for the if condition comes before the if condition.

lambda <args> : <statement1> if <condition > else ( <statement2> if <condition> else <statement3>)

Below is an example of a multiple condition lambda expression applied to a list of numbers in Python.

list_of_numbers = [0,4,2,5,9,1,10,15,14,8,2]
list_after_lambda = list(map(lambda x: x**2 if x < 3 else (x*4 if x < 8 else x), list_of_numbers))

print(list_after_lambda)

#Output:
[0, 16, 4, 20, 9, 1, 10, 15, 14, 8, 4]

Hopefully this article has been useful for you to learn how to use if in lambda functions in your Python code.

Other Articles You'll Also Like:

  • 1.  Print Approximately Equal Symbol in Python
  • 2.  Python if else do nothing – Using pass Statement to Do Nothing in Python
  • 3.  Change Python Turtle Shape Fill Color with fillcolor() Function
  • 4.  How to Filter pandas DataFrame by Date
  • 5.  pandas mean – Get Average of Series or DataFrame Columns
  • 6.  Using Python to Check If a Number is a Perfect Square
  • 7.  Difference Between print and return in Python
  • 8.  Using Python to Increment Dictionary Value
  • 9.  Check if Variable is Datetime in Python
  • 10.  How to Read CSV File from AWS S3 Bucket Using Python

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