Search code examples
python-3.xiterable

Apply multiple functions to multiple lists


a=[2,1,3]
b=[4,2,3]
c=[5,4,6]
....

a=set(sorted(map(lambda x: pow(x,3), a)))

How do I apply the same sequence of functions : set(), sorted() and pow() to multiple lists a, b and c... above without repeating x=set(... n times using map and lambda (or any other more efficient code)?

I know we can use a loop like my own answer below.

But what if we'd like to abstract this task further, so that a given list of functions [f1, f2, f3, f4] can be applied to a large number of lists a, b, c of a similar format? (Can itertools be used for the task?)


Solution

  • a=[2,1,3]
    b=[4,2,3]
    c=[5,4,6]
    
    new=[]
    for lst in [a,b,c]:
        new.append(set(sorted(map(lambda x: pow(x,3), lst))))
    
    a,b,c = new
    

    As suggested by @juanpa.arrivillaga in comments above, a loop can be used.