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

时间:2015-10-30 22:55:51

标签: python

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']

2 个答案:

答案 0 :(得分:9)

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)

答案 1 :(得分:1)

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

for x in interActor:
    if x in interActor1:
        anotherlist.append(x)
相关问题