Skip to content

Python map(lambda) function | Example code

  • by

Python map() function is an inbuilt function that takes a lambda function and applies that function to all the elements of specified iterable and returns map objects. The map is often used with a lambda function.

Read: Python Lambda Function

Python example map lambda function

Simple example code map function using lambda in Python. Get the square of the given list.

The map functions expect a function object and any number of iterable, such as the list, dictionary, etc.

nums1 = [1, 2, 3, 4, 5]
sq = list(map(lambda a: a * a, nums1))

print(sq)

Output:

Python map(lambda)

Another Example

The map() can accept more than one iterable. Using two lists and adding their values.

list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]

square_numbers = list(map(lambda x, y: x + y, list1, list2))

print(square_numbers)

Output: [7, 9, 11, 13, 15]

Do comment if you have any doubts or suggestions on this Python lambda tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading