We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

In Python, lambdas areanonymousfunctions that have a concise syntax and can be used with other helpful built-in functions.

python-lambda-with-map

Youll understand this better when we code examples.

it’s possible for you to code along in a Python REPL or using thisPython online compiler.

you could call the function with arguments and verify that its working correctly.

python-lambda-with-filter

However, we know that lambdas are anonymous functions, so you should avoid assigning them to a variable.

For the functionsquare(), the parameter isnumand the return value isnum*num.

Python functions can also take default values for parameters.

python-lambda-with-reduce

Lets modify the definition of theadd()function and set the default value of thenum2parameter to 10.

When Should You Use Lambda Functions in Python?

Notice the use of lambda function to define the squaring operation.

img

As themap()function returns a map object, we should cast it into a list.

Check out this tutorial on Python map function.

you might use Pythons built-infilter()function.

Thefilter()function takes in aconditionand aniterable:filter(condition, iterable).

The result contains only the elements in the original iterable that satisfy the condition.

you could cast the returned object into a Python iterable such as list.

To filter out all the even numbers, well retain only the odd numbers.

So the lambda expression should belambda num: num%2!=0.

The quantitynum%2is the remainder whennumis divided by 2.

It reduces the iterable by applying the function iteratively on the items of the iterable.

We define a lambda expression:lambda num1,num2:num1+num2, as the reducing sum function.

Here, f is the summing operation on two items of the list, defined by the lambda function.

If you are unsure about sums, you’re able to check this on understanding Pythons sum function.

If you need a sorted copy of the list, you might use thesorted()function.

Thekeyparameter is used to customize the sort.

Thereverseparameter can be set toTrueorFalse; the default value isFalse.

However, you may sometimes want to define some custom criterion for sorting.

Consider the following listfruits.

Suppose youd like to obtain a sorted copy of the list.

You should sort the strings according to the number of occurrences of p in them in the decreasing order.

Its time to use the optionalkeyparameter.

For the same sorting criterion, weve redefined thefruitslist.

Here, p occurs in the strings apple and grapes twice and once, respectively.

And it never occurs in the strings mango and melon.

Learn more about how to sort list in Python.

#2.Sorting a Python Dictionary

you’re able to also use lambdas when sorting Python dictionaries.

Consider the following dictionaryprice_dictthat contains items and their prices.

Wed like to sort by the value, which is the price of each item in the dictionary.

In each tuple in theprice_dict_itemslist, the item at index 1 is the price.

To learn more, check out this detailed guide on Python dictionary sorting by key and value.

Summing Up

And there you have it!

Youve learned how to define lambda functions and use them effectively with other Python built-in functions.

Heres a summary of the key takeaways:

Happy coding!

Heres more on Lambda, Python & Coding!