Python

 

 

 

 

Python - Map

 

Python's map() function is a built-in higher-order function that applies a given function to each item of an iterable (like a list, tuple, or set) and returns an iterator with the results. It is a convenient way to perform an operation on each element of a collection without the need for explicit loops.

 

The map() function takes two arguments: the first is the function you want to apply to each element of the iterable, and the second is the iterable itself. The input function should take a single argument, which is an element from the iterable, and return a value that will be included in the resulting iterator.

 

When you call the map() function, it iterates over the input iterable and applies the provided function to each element. The results are collected in a new iterator, which can be converted to a list, tuple, or another iterable type as needed.

 

In Python, you can pass any function, including lambda functions, as the input function for map(). Lambda functions are anonymous functions defined with the lambda keyword, and they can be used to create small, one-time-use functions directly within the map() call.

 

The map() function is particularly useful when you need to apply a specific operation or transformation to each element of a collection, such as scaling numerical values, converting data types, or extracting information from complex data structures. By using map() along with lambda functions or other custom functions, you can create more readable and concise code for element-wise operations.

 

 

 

Example

 

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

  • Applying a map with normal function with one argument  - Example 1
  • Applying a map with lambda function with one argument - Example 2
  • Applying a map with normal function with multiple argument  - Example 3
  • Applying a map with lambda function with multiple argument - Example 4
  • Printing out all the items in a Mapped Object - Example 5
  • Printing out all the items in a Mapped Object - Example 6
  • Printing out all the items in a Mapped Object - Example 7
  • Indexing items in a Mapped Object - Example 8

 

 

< Example 1 >

 

def f(x):

    y = x^2 + 2*x + 3

    return y

 

xList = [-3,-2,-1,0,1,2,3]

 

yList = map(f,xList)

print(yList)

print(list(yList))

 

Result :----------------------------------

<map object at 0x0306C770>

[2, -1, -4, 5, 6, 11, 8]

 

 

< Example 2 >

 

xList = [-3,-2,-1,0,1,2,3]

 

yList = map(lambda x: x^2 + 2*x + 3,xList)

print(yList)

print(list(yList))

 

Result :----------------------------------

<map object at 0x0314C7D0>

[2, -1, -4, 5, 6, 11, 8]

 

 

< Example 3 >

 

def timeStamp(y,m,d):

    tStamp = str(y) + "-" + m + "-" + str(d)

    return tStamp

 

yList = [2017,2017,2017,2017]

mList = ["Jan","Feb","Mar","Apr"]

dList = [1,3,6,7];

 

tStamp = map(timeStamp,yList,mList,dList)

print(tStamp)

print(list(tStamp))

 

Result :----------------------------------

<map object at 0x03000A30>

['2017-Jan-1', '2017-Feb-3', '2017-Mar-6', '2017-Apr-7']

 

 

< Example 4 >

 

yList = [2017,2017,2017,2017]

mList = ["Jan","Feb","Mar","Apr"]

dList = [1,3,6,7];

 

tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList)

print(tStamp)

print(list(tStamp))

 

Result :----------------------------------

<map object at 0x03091A30>

['2017-Jan-1', '2017-Feb-3', '2017-Mar-6', '2017-Apr-7']

 

 

< Example 5 >

 

yList = [2017,2017,2017,2017]

mList = ["Jan","Feb","Mar","Apr"]

dList = [1,3,6,7];

 

tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList)

 

for index, item in enumerate(tStamp):

   print(index,',',item)

    

 

Result :----------------------------------

0 , 2017-Jan-1

1 , 2017-Feb-3

2 , 2017-Mar-6

3 , 2017-Apr-7

 

 

< Example 6 >

 

yList = [2017,2017,2017,2017]

mList = ["Jan","Feb","Mar","Apr"]

dList = [1,3,6,7];

 

tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList)

 

for item in enumerate(tStamp):

   print(item)

 

 

Result :----------------------------------

(0, '2017-Jan-1')

(1, '2017-Feb-3')

(2, '2017-Mar-6')

(3, '2017-Apr-7')

 

 

< Example 7 >

 

yList = [2017,2017,2017,2017]

mList = ["Jan","Feb","Mar","Apr"]

dList = [1,3,6,7];

 

tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList)

 

for item in enumerate(tStamp):

    print(item[0],',',item[1])

 

 

Result :----------------------------------

0 , 2017-Jan-1

1 , 2017-Feb-3

2 , 2017-Mar-6

3 , 2017-Apr-7

 

 

< Example 8 >

 

yList = [2017,2017,2017,2017]

mList = ["Jan","Feb","Mar","Apr"]

dList = [1,3,6,7];

 

tStamp = map(lambda y,m,d : str(y) + "-" + m + "-" + str(d) ,yList,mList,dList)

 

tStampList = list(tStamp);

 

print(tStampList)

print(tStampList[0])

print(tStampList[1:3])

 

 

Result :----------------------------------

['2017-Jan-1', '2017-Feb-3', '2017-Mar-6', '2017-Apr-7']

2017-Jan-1

['2017-Feb-3', '2017-Mar-6']

 

 

< Example xx >