Python symmetric difference

Find if there are elements not common to both sets.

def symmetric_difference(iter_1, iter_2):
    """Return True when either parameter contains elements not in both."""
    return True if len(set(iter_1) ^ set(iter_2)) else False
print(symmetric_difference(['a'], ['a'])) # False
print(symmetric_difference(['a', 'b'], ['a', 'b'])) # False
print(symmetric_difference(['a', 'b', 'c'], ['a', 'b'])) # True
print(symmetric_difference(['a', 'b'], ['a', 'b', 'c'])) # True
If you liked this posting, please feel free to leave me a comment below.
View this page on GitHub.
Posted .


Leave a Reply