Map : Python Basics

prerna prakash
Mar 21, 2022

Map is another one of those things that make python “pythonic”.

Map applies a certain function to each item of an iterable. Map takes a minimum of 2 arguments : a function and a iterable (list, tuple, dict) and it returns a map object.

Example

This illustrates how map reduces lines of code and makes it more pythonic

Map in often used in conjunction with lambda. Below example shows 3 different types of functions passed to map :

  1. with a python pre-defines function
  2. with a lambda function
  3. with a user defines function (def keyword)
3 different functions passed to map in python

Lastly let’s take a scenario where the function passed to map requires 2 or more arguments, here we simply need to pass all the iterables instead of just one.

--

--

prerna prakash

A lover of good stories | Data Engineer | Passionate Traveller | An obsessive reader