how to solve time limit exceed thaf face me while solving on codeforces?

at the last days when I solved any problem and the input is very large it get time exceed so I used to not do for loops and I know that isn't the problem so how to solve this one ?

this the problem: http://codeforces.com/problemset/problem/227/B

def mod():
    m=int(input())
    x=list(map(int,input().split()))
    o=int(input())
    needed=list(map(int,input().split()))
    vasya=[]
    petya=[]
    c=list(dict.fromkeys(x))
    for i in range(len(needed)):
        v=list(zip(*filter(lambda x: x[1] ==needed[i], enumerate(x))))[0]
        vasya.append(*v)
    x.reverse()
    for j in range(len(needed)):
        f=list(zip(*filter(lambda x: x[1] ==needed[j], enumerate(x))))[0]
        petya.append(*f)
    l=list(map(lambda x:x+1,vasya))
    k=list(map(lambda x:x+1, petya))
    print(sum(l),sum(k),sep=" ")

mod()

I want solution for this problem on general how to make my code right without change my logic