Python check if object is in list of objects

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-04-01T08:35:15Z Indexed on 2010/04/01 8:43 UTC
Read the original article Hit count: 193

Filed under:

Hi, I have a list of objects in Python. I then have another list of objects. I want to go through the first list and see if any items appear in the second list.

I thought I could simply do

for item1 in list1:
    for item2 in list2:
        if item1 == item2:
            print "item %s in both lists"

However this does not seem to work. Although if I do:

if item1.title == item2.title:

it works okay. I have more attributes than this though so don't really want to do 1 big if statement comparing all the attributes if I don't have to.

Can anyone give me help or advise on what I can do to find the objects which appear in both lists.

Thanks

© Stack Overflow or respective owner

Related posts about python