Search code examples
pythonfunctionmapreducereturn

Function won't return value


I have an assignment to code my own map function and I'm not sure why it's not returning any value. Here's the code below:

def mymap(func, lst):
    new_lst = []
    for items in lst:
        new_lst.append(func(items))
    return new_lst

mymap(abs, [3,-1, 4, -1, 5, -9])

It should return [3, 1, 4, 1, 5, 9], but when I run it, it doesn't return anything.


Solution

  • You need to add print in:

    def mymap(func, lst):
        new_lst = []
        for items in lst:
            new_lst.append(func(items))
        return new_lst
    
    print(mymap(abs, [3,-1, 4, -1, 5, -9]))
    

    Outputs:

    [3, 1, 4, 1, 5, 9]