# Python program to demonstrate working# of map.# Return double of ndefaddition(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']]
# The map function applies a function to every item in a list,# and returns a new list.
numbers =[0,-1,2,3,-4]defsquare_func(n):return n*n
new_numbers =list(map(square_func, numbers))#new_numbers: [0, 1, 4, 9, 16]
# Python program to demonstrate working # of map. # Return double of n defaddition(n):return n + n
# We double all numbers using map()
numbers =(1,2,3,4)
result =map(addition, numbers)print(list(result))
defcalculateSquare(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)
# Let's define general python function>>>defdoubleOrNothing(num):...return num *2# now use Map on it.>>>map(doubleOrNothing,[1,2,3,4,5])<mapobject 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]
# Python program to demonstrate working # of map. # Return double of n defaddition(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]
# Python program to demonstrate working# of map.# Return double of ndefaddition(n):return n + n
# We double all numbers using map()
numbers =(1,2,3,4)
result =map(addition, numbers)print(list(result))
'''
Map:
A standard function that accepts at least "two-arguments",
a function and an "iterable"
Iterable - something that can be iterated over (list, dictionary,
strings, sets, tuples)
runs the Lambda for each value in the iterable and
returns a map object which can be converted into another data structure
Keyword:
map(function/lambda func., variable)
'''# Example:
nums =[1,2,3,4]
power =map(math.pow(2), nums)print(list(power))# 1, 4, 9, 16
powerLamb =map(lambda x: x**2, nums)# 1, 4, 9, 16
map()is a higher-order built-in function that takes a function and iterable as inputs,and returns an iterator that applies the function to each element of the iterable.
//if you want to take single intinput
a=int(input())//if you want n elements of array a asinputfrom console/user
a =list(map(int,input().strip().split()))# u can also covert it to set,tuple etc # ex. set(map(int, input().strip().split()))
NOTE: suppose if you want a listwith duplicates removed
list(set(map(int,input().strip().split())))
also note mapis a method andisnot hashmap which is actually disct in python.and ommitting .strip()in 2nd argument of map func might also work.# more explaination of above:
https://www.quora.com/What-does-the-following-line-mean-in-Python-list-map-int-input-strip-split-I
# Python program to demonstrate working# of map.# Return double of ndefaddition(n):return n + n
# We double all numbers using map()
numbers =(1,2,3,4)
result = addition(numbers)print(list(result))
price_list=[120,250,133,420,145,369,700]# given alist of pricesdefsale(price):# function for add discount in sale for evry price
discount=(15/100)# discount 15%
new_price = price*discount
# new pricereturn new_price
new_price_list=list(map(sale,price_list))# 120*15% , 250*15)3*15%# use map function take evry price in price list# add evry price to slae funtion# add discount to this price return new price # add evry new price in new_price_listprint(f"price list in sale {new_price_list}")#print new list of prices
#The map function is used to do a certain function to a certain iterable#It takes a function and an iterable
numbers =[1,2,3,4,5,6,7,8,9,10]
x =map(lambda nom : nom*nom, numbers)print(list(x))#So here my function is the lambda and the iterable is the numbers list#And with this I apply nom*nom to every item to the list# if I didn't put the list function before x it would print map object at .....