Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

map example in python

# 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']]
 
PREVIOUS NEXT
Tagged: #map #python
ADD COMMENT
Topic
Name
4+1 =