How to use map ,list function in python||Map,list || Python Tutorial || Learn Python Programming

Veröffentlicht am: 01 Januar 1970
auf dem Kanal: Saayan Biswas
29
1

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.)

Syntax :

map(fun, iter)
Parameters :

fun : It is a function to which map passes each element of given iterable.
iter : It is a iterable which is to be mapped.

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



Returns :

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

NOTE : The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set) .

CODE 1

filter_none
edit
play_arrow

brightness_4
Python program to demonstrate working
of map.

Return double of n
def addition(n):
return n + n

We double all numbers using map()
numbers = (1, 2, 3, 4)
The map() function executes a specified function for each item in a iterable. The item is sent to the function as a parameter.
How to use map ,list function in python||Map,list || Python Tutorial || Learn
Python Programming Saayan Biswas
result = map(addition, numbers)
print(list(result))
Output :

[2, 4, 6, 8]

CODE 2
We can also use lambda expressions with map to achieve above result.

filter_none
edit
play_arrow

brightness_4
Double all numbers using map and lambda

numbers = (1, 2, 3, 4)
result = map(lambda x: x + x, numbers)
print(list(result))
Output :

[2, 4, 6, 8]

CODE 3

filter_none
edit
play_arrow

brightness_4
Add two lists using map and lambda

numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]

result = map(lambda x, y: x + y, numbers1, numbers2)
print(list(result))

~-~~-~~~-~~-~
Please watch: "How to split strings||pycharm stuffs"
   • How to split strings||pycharm stuffs  
Dont click:--https://www.youtube.com/channel/UCAq6...
~-~~-~~~-~~-~


Auf dieser Seite können Sie das Online-Video How to use map ,list function in python||Map,list || Python Tutorial || Learn Python Programming mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Saayan Biswas 01 Januar 1970 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 29 Mal angesehen und es wurde von 1 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!