• 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 / 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.  How to Find the Longest String in List in Python
  • 2.  Using Lambda Expression with max() in Python
  • 3.  Reverse a List in Python Without Reverse Function
  • 4.  Using Python to Remove First Character from String
  • 5.  Find Magnitude of Complex Number in Python
  • 6.  Date Format YYYYMMDD in Python
  • 7.  Python ljust Function – Left Justify String Variable
  • 8.  Unset Environment Variables in Python
  • 9.  pandas nsmallest – Find Smallest Values in Series or Dataframe
  • 10.  Create Empty String Variable in 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

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