Python map() with Lambda Function

Using lambda with map() function is the most common way of usage in Python to perform transformations on iterable objects (e.g., list, tuple, set).

What is map() Function?

The map() function in Python is used to apply the transformation to an iterable object like a list, tuple, or set e.t.c, this applies the given transformation function to each item of an iterable (e.g., list, tuple, set). You can combine this with a lambda function. Here is the syntax of the map().


# Syntax of map() function
map(function, iterable, [iterable1, iterable2, ...])

Here is an example of map().


# Create list of numbers
numbers = [2, 4, 6, 8, 5]
print("Original:", numbers)

# Create function square
def square(x):
    return x * x

# Use map() function to apply a function
square_numbers = map(square, numbers)
result = list(square_numbers)
print("Result:",result)

# Output:
# Original: (2, 4, 6, 8, 5)
# Result: [4, 16, 36, 64, 25]

What is Lambda Function?

A Python lambda function is a small anonymous function, an anonymous meaning function without a name. The Lambda functions are mainly used in combination with the functions filter(), map(), and reduce(). This function can take any number of arguments, but can only have one expression and they can be used wherever function objects are required. Here is the syntax of the lambda.


# Syntax of lambda
lambda arguments: expression

Here is an example of lambda.


# Lambda example with single argument
square = lambda x: x * x
print(square(4))

# Output:
# 16

1. Syntax of Lambda & map()

The following is the syntax of using python lambda with map() function.


# Syntax
map(lambda arguments: expresion, iterable, [iterable1, iterable2, ...])

Here,

  • lambda – is a python keyword that is used to create a lambda function.
  • arguments – arguments to the lambda function.
  • expression – expression to execute by using the arguments.
  • iterable – It is iterable which is the collection of items on which the function will be applied. You can use one or more iterables.

2. Apply Lambda with Map on Single Iterable

In this example, the lambda function takes one argument x and returns its square. The map() function applies this python lambda function to each item of the numbers list, and the result is a new iterator containing the squared values of each number. The list() function is used to convert the iterator to a list so that we can print the results.


# Create numbers list
numbers = [2, 4, 5, 6, 3]

# Use map() with lambda that takes single argument
squared_result = list(map(lambda x: x**2, numbers))
print(squared_result) 

# Output: 
# [4, 16, 25, 36, 9]

The above code can also write as below. Here, I create a square_fun variable and assigned a python lambda to it, and used this function on map().


# Create lambda function and assign it to variable
square_fun = lambda x: x**2
squared_result = list(map(square_fun, numbers))

3. Using with Multiple Iterables

Similarly, you can also create a lambda function with multiple arguments, let’s take another example with two arguments. To pass values to these arguments you need to use two iterables.


# Create two lists with numbers 
numbers1 = [2, 4, 5, 6, 3]
numbers2 = [1, 3, 2, 2, 4]

# Create lambda function that takes two arguments
add_fun = lambda x, y: x+y

$ Use lambda with map() function
add_result = list(map(add_fun, numbers1, numbers2))
print(add_result) 

# Output:
# [3, 7, 7, 8, 7]

Here, the lambda function takes two arguments x, y and adds their values. The map() function applies this lambda function to each item of the numbers1 & numbers2 lists, and the result is a new iterator containing the sum of both numbers element-wise.

4. Using Python Function with map() and Lambda

So far you have seen calling the custom functions, now let’s use the python lower() function with lambda and map(). Here, I convert the list of string values to lowercase.


# Create string list
myStrings = ["Python","Java","PHP","Scala"]
print("Original:",myStrings)

# Use lower() function
lower_result = list(map(lambda x: x.lower(), myStrings))
print("Result:",lower_result)

# Output: 
# Original: ['Python', 'Java', 'PHP', 'Scala']
# Result: ['python', 'java', 'php', 'scala']

Conclusion

In this article, you have learned how to use the python lambda function along with a map() to utilize the full potential of these. The map() enables lambda to use iterables like list, set, and tuple. Also learned how to use these two together with single and multiple iterables.

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply