26
loading...
This website collects cookies to deliver better user experience
map(function, iterable[...iterable(N)])
def for_loop():
number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
square_list = []
for number in number_list:
square_list.append(number ** 2)
print(f"squared => {square_list}")
if __name__ == "__main__":
for_loop()
Output:
squared => [1, 4, 9, 16, 25, 36, 49, 64, 81]
def loop_fun():
number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
square_list = [number**2 for number in number_list]
print(f"squared => {square_list}")
Output:
squared => [1, 4, 9, 16, 25, 36, 49, 64, 81]
def square(n):
return n ** 2
def map_fun():
list_number = [1, 2, 3, 4, 5, 6, 7, 8, 9]
square_list = list(map(square, list_number))
print(f"squared => {square_list}")
Output:
squared => [1, 4, 9, 16, 25, 36, 49, 64, 81]
def map_fun():
list_string = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
list_int = list(map(int, list_string))
print(f"converted list => {list_int}")
Output:
converted list => [1, 2, 3, 4, 5, 6, 7, 8, 9]
def map_fun():
list_int = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list_float = list(map(float, list_int))
print(f"converted list => {list_float}")
def map_fun():
list_numbers = [-5, 2, -8, 11, -2.17, -1.99, 6, 4]
abs_list = list(map(abs, list_numbers))
print(f"abs values => {abs_list}")
Output:
abs values => [5, 2, 8, 11, 2.17, 1.99, 6, 4]
def map_fun():
number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list_square = list(map(lambda number: number ** 2, number_list))
print(f"lambda squared => {list_square}")
Output:
lambda squared => [1, 4, 9, 16, 25, 36, 49, 64, 81]
def map_fun():
list_num_1 = [8, 4, -7, 3, -2]
list_num_2 = [3, -1, 9, -3, 5]
list_sum = list(map(lambda t1, t2: t1+t2, list_num_1, list_num_2))
print(list_sum)
Output:
sum of the lists => [11, 3, 2, 0, 3]
def map_fun():
fruits = ["apple", "banana", "grape", "melon", "avocado", "mango", "orange"]
upper_fruits = list(map(lambda fruit: fruit.upper(), fruits))
capitalize_fruits = list(map(lambda fruit: fruit.capitalize(), fruits))
print(f"UPPER => {upper_fruits}")
print(f"Capitalize => {capitalize_fruits}")
Output:
UPPER => ['APPLE', 'BANANA', 'GRAPE', 'MELON', 'AVOCADO', 'MANGO', 'ORANGE']
Capitalize => ['Apple', 'Banana', 'Grape', 'Melon', 'Avocado', 'Mango', 'Orange']
s_m = ["string/", "manipulation/"]
list_string = list(map(lambda t: t.strip("/"), s_m))
print(f"list string => {list_string}")
Output:
list string => ['string', 'manipulation']
fruits = ["apple", "banana", "grape", "melon", "avocado", "mango", "orange"]
upper_fruits = list(map(str.upper, fruits))
capitalize_fruits = list(map(str.capitalize, fruits))
print(f"UPPER => {upper_fruits}\nCapitalize => {capitalize_fruits}")
Output:
UPPER => ['APPLE', 'BANANA', 'GRAPE', 'MELON', 'AVOCADO', 'MANGO', 'ORANGE']
Capitalize => ['Apple', 'Banana', 'Grape', 'Melon', 'Avocado', 'Mango', 'Orange']
s = "string manipulation using map"
list_split = list(map(str.split, s))
print(f"split => {list_split}")
Output:
split => [['s'], ['t'], ['r'], ['i'], ['n'], ['g'], [], ['m'], ['a'], ['n'], ['i'], ['p'], ['u'], ['l'], ['a'], ['t'], ['i'], ['o'], ['n']]
def map_fun():
car_trademarks = {
"car1": "BMW",
"car2": "Audi",
"car3": "Bentley",
"car4": "Mercedes",
"car5": "Porsche"
}
trademarks_list = list(map(lambda index: car_trademarks.get(index), car_trademarks))
print(trademarks_list)
Output:
the car list => ['BMW', 'Audi', 'Bentley', 'Mercedes', 'Porsche']
class Palindrome(object):
def __init__(self, str_obj):
self.list_obj = list(map(lambda t: t.strip(), str(str_obj)))
def _isPalindrome(self):
list_temp = self.list_obj
list_temp = list_temp[::-1]
if list_temp == self.list_obj:
return True
return False
def __str__(self):
if self._isPalindrome():
return f"The object {self.list_obj} is palindrome !"
else:
return f"The object {self.list_obj} is not palindrome !"
if __name__ == "__main__":
string = "tenet"
p = Palindrome(string)
print(p)
Output:
The object ['t', 'e', 'n', 'e', 't'] is palindrome !