Python – Use ‘set’ to find the different items in list

The docs are a good place to start. Here are a couple examples that might help you determine how you want to compare your sets.

To find the intersection (items that are in both sets):

>>> a = set([1, 2, 3, 4, 5, 6])
>>> b = set([4, 5, 6, 7, 8, 9])
>>> a & b
set([4, 5, 6])

To find the difference (items that only in one set):

>>> a = set([1, 2, 3, 4, 5, 6])
>>> b = set([4, 5, 6, 7, 8, 9])
>>> a - b
set([1, 2, 3])
>>> b - a
set([7, 8, 9])

To find the symmetric difference (items that are in one or the other, but not both):

>>> a = set([1, 2, 3, 4, 5, 6])
>>> b = set([4, 5, 6, 7, 8, 9])
>>> a ^ b
set([1, 2, 3, 7, 8, 9])

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)