Python - Using set to isolate differences

Introduction

Sets can isolate differences in lists, strings, and other iterable objects.

Convert them to sets and take the difference.

Demo

print( set([1, 3, 5, 7]) - set([1, 2, 4, 5, 6]) )          # Find list differences 
print( set('abcdefg') - set('abdghij') )                   # Find string differences 
print( set('test') - set(['h', 'a', 'm']) )                # Find differences, mixed 
print( set(dir(bytes)) - set(dir(bytearray)) )             # In bytes but not bytearray 
print( set(dir(bytearray)) - set(dir(bytes)) )

Result

Related Topic