Cara menggunakan python map object

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




# Python program to demonstrate working

# of map.

  

# Return double of n

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

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

  

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

Returns a list of the results after applying the given function  
to each item of a given iterable (list, tuple etc.) 
9
[2, 4, 6, 8]
0
[2, 4, 6, 8]
1
[2, 4, 6, 8]
2
[2, 4, 6, 8]
3
[2, 4, 6, 8]
4
[2, 4, 6, 8]
3
[2, 4, 6, 8]
6
[2, 4, 6, 8]
3
[2, 4, 6, 8]
8
[2, 4, 6, 8]
9

[2, 4, 6, 8]
0
[2, 4, 6, 8]
0
[2, 4, 6, 8]
2
[2, 4, 6, 8]
3

[2, 4, 6, 8]
4
[2, 4, 6, 8]
1
[2, 4, 6, 8]
6
[2, 4, 6, 8]
7

Output :

[2, 4, 6, 8]

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

Cara menggunakan python map object




[2, 4, 6, 8]
8

  

Returns a list of the results after applying the given function  
to each item of a given iterable (list, tuple etc.) 
9
[2, 4, 6, 8]
0
[2, 4, 6, 8]
1
[2, 4, 6, 8]
2
[2, 4, 6, 8]
3
[2, 4, 6, 8]
4
[2, 4, 6, 8]
3
[2, 4, 6, 8]
6
[2, 4, 6, 8]
3
[2, 4, 6, 8]
8
[2, 4, 6, 8]
9

[2, 4, 6, 8]
0
[2, 4, 6, 8]
0
[2, 4, 6, 8]
2
[2, 4, 6, 8]
1
[['s', 'a', 't'], ['b', 'a', 't'], ['c', 'a', 't'], ['m', 'a', 't']]
5
[['s', 'a', 't'], ['b', 'a', 't'], ['c', 'a', 't'], ['m', 'a', 't']]
6
Returns a list of the results after applying the given function  
to each item of a given iterable (list, tuple etc.) 
5
[['s', 'a', 't'], ['b', 'a', 't'], ['c', 'a', 't'], ['m', 'a', 't']]
8

[2, 4, 6, 8]
4
[2, 4, 6, 8]
1
[2, 4, 6, 8]
6
[2, 4, 6, 8]
7

Output :

[2, 4, 6, 8]

 
CODE 3




# Python program to demonstrate working3

  

# Python program to demonstrate working5

[2, 4, 6, 8]
0 # Python program to demonstrate working7
[2, 4, 6, 8]
2
[2, 4, 6, 8]
3
[2, 4, 6, 8]
4
[2, 4, 6, 8]
3
[2, 4, 6, 8]
6# of map.3

# of map.4

[2, 4, 6, 8]
0 # Python program to demonstrate working7
[2, 4, 6, 8]
8
[2, 4, 6, 8]
3# of map.9
[2, 4, 6, 8]
3 1# of map.3

  

[2, 4, 6, 8]
0
[2, 4, 6, 8]
0
[2, 4, 6, 8]
2
[2, 4, 6, 8]
1
[['s', 'a', 't'], ['b', 'a', 't'], ['c', 'a', 't'], ['m', 'a', 't']]
5  9
Returns a list of the results after applying the given function  
to each item of a given iterable (list, tuple etc.) 
5 # Return double of n1

[2, 4, 6, 8]
4
[2, 4, 6, 8]
1
[2, 4, 6, 8]
6
[2, 4, 6, 8]
7

Output :

[5, 7, 9]

 
CODE 4




# Return double of n6

# Return double of n7

[2, 4, 6, 8]
0 # Python program to demonstrate working7
Returns a list of the results after applying the given function  
to each item of a given iterable (list, tuple etc.) 
00
[2, 4, 6, 8]
3
Returns a list of the results after applying the given function  
to each item of a given iterable (list, tuple etc.) 
02
[2, 4, 6, 8]
3
Returns a list of the results after applying the given function  
to each item of a given iterable (list, tuple etc.) 
04
[2, 4, 6, 8]
3
Returns a list of the results after applying the given function  
to each item of a given iterable (list, tuple etc.) 
06# of map.3

Apa fungsi map pada Python?

map() adalah salah satu built-in function di Python yang dapat digunakan untuk menerapkan sebuah fungsi pada semua elemen dari objek yang bertipe iterable seperti list, tuple, dan sebagainya. Hasil dari map() adalah berupa objek map yang dapat dikonversi kembali menjadi objek list, tuple, dan lainnya.

Apa yang dimaksud dengan Python Maps?

Python Map () adalah metode terintegrasi yang memungkinkan semua objek diinterpretasikan dan diterjemahkan ke dalam iterable, bukan loop eksplisit, biasanya disebut sebagai mapping.

Apa fungsi append pada Python?

Append. Salah satu fitur dalam array python yang cukup sering digunakan adalah fungsi append. Fungsi append ini berguna untuk menambahkan nilai array pada urutan terakhir. Fungsi ini sedikit berbeda dengan fungsi insert, dimana fungsi insert bisa menambahkan nilai array pada posisi tertentu.

Apa yang dimaksud dengan list di Python?

List adalah tipe data yang paling serbaguna dalam bahasa pemrograman Python. List ditulis sebagai daftar nilai yang dipisahkan koma (item) antara tanda kurung siku. Dalam membuat list pada Python sangatlah sederhana. Tinggal memasukkan berbagai nilai yang dipisahkan dengan tanda koma di antara tanda kurung siku.