Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare two list ,object instances in python [closed]

Say if I have:

list1 = [1,6]
list2 = [1]

I want to do something if list values match!

Compare it and do stuff after that

like image 430
user2481309 Avatar asked Jun 21 '13 13:06

user2481309


1 Answers

Mmm, like this?

if list1 == list2: # compare lists for equality
    doStuff()      # if lists are equal, do stuff after that   

Of course, you need to clarify what do you mean by "if lists values match". The above will check to see if both lists have the same elements, in the same position - that is, if they're equal.

EDIT:

The question is not clear, let's see some possible interpretations. To check if all elements in list1 are also in list2 do this:

if all(x in list2 for x in list1):
    doStuff()

Or to do something with each element in list1 that also belongs in list2, do this:

for e in set(list1) & set(list2):
    doStuff(e)
like image 88
Óscar López Avatar answered Sep 21 '22 06:09

Óscar López