How to Use map Function in Python


In this example, we will learn how to use the map function in Python.

Source Code

For list [1, 2, 3, 4, 5, 6, 7, 8, 9], if you want to square every element of the list, you can use the map() function:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

def f1(x):
    return x*x

print(list(map(f1, [1, 2, 3, 4, 5, 6, 7, 8, 9])))

Output:

[1, 4, 9, 16, 25, 36, 49, 64, 81]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments