map pyton code example
Example 1: map in python 3
map(function, iterable, ...)
Example 2: python dictionary map function
dictionary = {k: f(v) for k, v in dictionary.items()}
Example 3: map in python
'''try this instead of defining a function'''
a = [1,2,3,4]
a = list(map(lambda n: n+n, a))
print(a)
Example 4: map function in python
def multiply_by2(li):
new_list = []
for number in li:
new_list.append(number*2)
return new_list
print(multiply_by2([1, 2, 3]))
def multiply_by_2(item):
return item*2
print(list(map(multiply_by_2, [1, 2, 3])))
usernames = ["RN_Tejas", "RN_Tejasprogrammer", "Rajendra Naik", "yooo", "Brainnnni", "Ouuuuut"]
def no_lower(item):
return item.upper()
print(list(map(no_lower, usernames)))
list_squares = list(range(1, 101))
print(list_squares)
def square(number):
return number**2
square_list = list(map(square, list_squares))
def square_root(number):
return number // number
print(list(map(square_root, square_list)))
def no_upper(string):
return string.lower()
print(list(map(no_upper, 'TEJAS')))