Search code examples
pythonstringsetset-difference

Python set difference of strings


I encountered the below peculiar behaviour when creating difference of string sets in Python:

set(['a', 'b']) - set(['a'])   # results in {'b'} as expected
set(['a', 'b']) - set('a')     # results in {'b'} as expected
set(['a.', 'b']) - set(['a.']) # results in {'b'} as expected
set(['a.', 'b']) - set('a.')   # surprisingly results in {'a.', 'b'}!

Why is it that in the last case 'a.' is not subtracted from the set? Since the difference between the second and the fourth case is the dot, I reckon that is the culprit. Using double quotes gives the same behaviour.


Solution

  • Your last set is interpreted as {'a', '.'}. So your set operation will not exclude 'a.'

    It's because set will iterate through the input, and the iteration over a string is by char.