Index

  1. Definition
  2. Syntax
  3. Parameter Values (input)
  4. Return Values (output)

Definition

map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.)

oss:

if you what to print the result of map you have to translate i to a list or tuple or set (ex: list(map) ,set(map) …)

  • this because map returns the address of map object example

Syntax

map(fun, *iterables)

Parameter

  • fun: - It is a function to which map passes each element of given iterable.

  • iterables: It is iterable which is to be mapped.

    NOTE: You can pass one or more iterable to the map() function.

Return

  • Returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)

Example

One Iterator:

def double(n):
    return n*2
 
numbers = (1, 2, 3, 4)
 
list(map(double, numbers)) #Output: [2, 4, 6, 8]

labda version:

numbers = (1, 2, 3, 4)
 
list(map(lambda x: x*2, numbers)) #Output: [2, 4, 6, 8]

Why list(map(...))

because if you print(map) without the list you will print

Two Iterator:

numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
 
list(map(lambda x, y: x + y, numbers1, numbers2))  #Output: [5, 7, 9]

Advanced Two Iterators:

def upper_lower(inter1, iter2):
	return iter1.apper(), iter2.lower()
 
 
list(map(upper_lower, ["jAx", "KeEpEr"], ["ToNy", "sTaRk"]))
#Output: [("JAX", "keper"), ("TONY", "stark")]

labda version:

list(map(lambda x,y: (x.upper(),y.lower()),  ["jAx", "KeEpEr"], ["ToNy", "sTaRk"]))
#Output: [('JAX', 'tony'), ('KEEPER', 'stark')]