208k views
1 vote
Complete the implementation of the function get_a11_sum that takes a list of numbers functional operations to returns the sum of the absolute values from list

import functools
def get al1_sum(lst) :
≫ get_al1_sum([5,-1, 12,−10,2,8])
28

New_lst = [abs(num) for ____ in ____ ]
Return ____ (lamda a, b: ____ . )

1 Answer

5 votes

Final answer:

The function get_al1_sum should use a list comprehension and functools.reduce to calculate the sum of absolute values from a list.

Step-by-step explanation:

The function get_al1_sum is intended to return the sum of the absolute values of a list of numbers. To complete the implementation, one needs to use a list comprehension to create a list of the absolute values of the numbers in the given list and then apply the functools module to reduce the new list to a single sum. Here's how you can complete the function:

import functools

def get_al1_sum(lst):
new_lst = [abs(num) for num in lst]
return functools.reduce(lambda a, b: a + b, new_lst)

When you call get_al1_sum([5, -1, 12, -10, 2, 8]), it will return 28, which is the sum of |5| + |-1| + |12| + |-10| + |2| + |8|.

User Bezejmeny
by
8.1k points