Skip to content
Advertisement

‘list’ object is not callable for find same elements in two different list

I’m new in Python, I don’t know where I made mistakes

def q(listA, listB):

    list1=[]
    for a in range(len(listA)):
        for b in range(len(listB)):
            if listA(a)==listB(b):
                list1.append(a)
    list2=[]
    for x in range(len(listb)):
        for y in range(len(listA)):
            if listB(x)==listA(y):
                list2.append(x)
    
    
    return list(zip(list1,list2))

pairAPy = [1,2,3,4]
pairBPy = [1,5,7,2]
print(q(pairAPy,pairBPy))
Line 21 print(q(pairAPy,pairBPy))  
 Line 8             if listA(a)==listB(b):  
 TypeError: 'list' object is not callable

and the answer should return [(0, 0), (2, 1), (3, 3)].

Advertisement

Answer

You accidentally used (a) and (b) instead of [a] and [b]. Your code should look like this:

def q(listA, listB):

    list1 = []
    for a in range(len(listA)):
        for b in range(len(listB)):
            **if listA[a] == listB[b]:
                list1.append(a)
    list2=[]
    for x in range(len(listb)):
        for y in range(len(listA)):
            if listB[x] == listA[y]:
                list2.append(x)
    
    
    return list(zip(list1, list2))

pairAPy = [1,2,3,4]
pairBPy = [1,5,7,2]
print(q(pairAPy, pairBPy))
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement