Map Function in Python:
xxxxxxxxxx
# Welcome to Python World!
# return a list of the price for these items
items = [{"name": "Mango", "price": 11.26},
{"name": "Apple", "price": 8.65},
{"name": "Watermelon", "price": 4.31},
{"name": "Orange", "price": 5.96}]
# Using programming fundamentals and intuition:
def get_price_values(some_list):
local_list = list()
for item in some_list:
local_list.append(item["price"])
return local_list
# Using functional programming intuition: map(...)
def get_price_with_lambda(some_list):
return list(map(lambda item: item["price"], items))
def get_price_(some_list):
def local_func(item):
return item["price"]
return list(map(local_func, items))
def test_():
assert get_price_values(items) == [11.26, 8.65, 4.31, 5.96]
assert get_price_with_lambda(items)== [11.26, 8.65, 4.31, 5.96]
assert get_price_(items) == [11.26, 8.65, 4.31, 5.96]
print("Passed!")
test_() # Passed!
Why should you use map?
That's like asking why you should use for or while...
- it is part of the language.
- it saves you time, and code
- making use of Python's powerful Higher Order Functions.
Unfortunately, it is no more efficient than the conventional way.
Conventional way applies for just about any language.
xxxxxxxxxx
# 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)
result = map(addition, numbers)
print(list(result))
# Output :-
# 1 + 1 = 2 / 2 + 2 = 4 / 3 + 3 = 6 / 4 + 4 = 8
[2, 4, 6, 8]
# Example With Strings :-
# List of strings
my_list = ['sat', 'bat', 'cat', 'mat']
# map() can listify the list of strings individually
test = list(map(list, my_list))
print(test)
# Output :-
# listify the list of strings individually Like :-
# 'sat' 3 Letters ==> The Function is Offered Individually :- 's', 'a', 't'
[['s', 'a', 't'], ['b', 'a', 't'], ['c', 'a', 't'], ['m', 'a', 't']]
xxxxxxxxxx
# The map function applies a function to every item in a list,
# and returns a new list.
numbers = [0, -1, 2, 3, -4]
def square_func(n):
return n*n
new_numbers = list(map(square_func, numbers))
#new_numbers: [0, 1, 4, 9, 16]
xxxxxxxxxx
# 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)
result = map(addition, numbers)
print(list(result))
xxxxxxxxxx
def calculateSquare(n):
return n*n
numbers = (1, 2, 3, 4)
result = map(calculateSquare, numbers)
print(result)
# converting map object to set
numbersSquare = list(result)
print(numbersSquare)
xxxxxxxxxx
#Methodmap will make your code more readable
#We multiply all the values in the list by two
numbers=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list(map(lambda num: num*2, numbers)))
#If you run the code, you will see that you were able to do this without using a loop
#GOOD LUCK
xxxxxxxxxx
# Let's define general python function
>>> def doubleOrNothing(num):
return num * 2
# now use Map on it.
>>> map(doubleOrNothing, [1,2,3,4,5])
<map object at 0x7ff5b2bc7d00>
# apply built-in list method on Map Object
>>> list(map(doubleOrNothing, [1,2,3,4,5])
[2, 4, 6, 8, 10]
# using lambda function
>>> list(map(lambda x: x*2, [1,2,3,4,5]))
[2, 4, 6, 8, 10]
xxxxxxxxxx
# 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)
result = map(addition, numbers)
print(list(result))
# result [2, 4, 6, 8]
xxxxxxxxxx
#### Core python use case #####
#map(func_name, some_list)
items = [1, 2, 3, 4]
list(map(lambda x: x + 2 , items)) ## [3, 4, 5, 6]
#### Dataframe Application #####
# Method 1
df["col"].apply(lambda x: x+1)
# Method 2
genders = {'James': 'Male', 'Jane': 'Female'}
df['gender'] = df['name'].map(genders)