Ezana Tesfaye
Ezana Tesfaye

Reputation: 19

How to check if an element in the list exists in another list

How to check if an element in the list exists in another list?And if it does append it to another list.How Can i make it to get all the values in a list?

common=[]

def findCommon(interActor,interActor1):
    for a in interActor:
        if a in interActor1:
            common.append(a)
    return common
interActor=['Rishi Kapoor','kalkidan','Aishwarya']
interActor1=['Aishwarya','Suman Ranganathan','Rishi Kapoor']

Upvotes: 1

Views: 117

Answers (2)

Eugene Soldatov
Eugene Soldatov

Reputation: 10145

You can do it using list comprehensions:

common = [x for x in iter_actor1 if x in iter_actor2]

or using sets:

common = set(iter_actor1).intersection(iter_actor2)

Upvotes: 9

GLHF
GLHF

Reputation: 4044

interActor=['Rishi Kapoor','kalkidan','Aishwarya']
interActor1=['Aishwarya','Suman Ranganathan','Rishi Kapoor']
anotherlist = []

for x in interActor:
    if x in interActor1:
        anotherlist.append(x)

Upvotes: 1

Related Questions