Joseph A. Joseph A. - 1 year ago 74
Python Question

Find the element "left" in a list

I have the list

list1 = ["Hello", "Goodbye", "Ohai"]

I have another list,
list2 = ["Hello", "Goodbye"]

I want to get the remaining items in
, in this case this would be

Answer Source
>>> list1 = ["Hello", "Goodbye", "Ohai"]
>>> list2 = ["Hello", "Goodbye"]
>>> set(list1) ^ set(list2)

If you are supposed to use lists only, you can iterate:

>>> [item for item in list1 if item not in list2]

But why do that when you have sets ;-)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download