This question is the first one that shows up on Google when one looks up "Python how to add elements to set", so it's worth noting explicitly that, if you want to add a whole string to a set, it should be added with .add()
, not .update()
.
Say you have a string foo_str
whose contents are 'this is a sentence'
, and you have some set bar_set
equal to set()
.
If you do
bar_set.update(foo_str)
, the contents of your set will be {'t', 'a', ' ', 'e', 's', 'n', 'h', 'c', 'i'}
.
If you do bar_set.add(foo_str)
, the contents of your set will be {'this is a sentence'}
.