How do I add two sets?

GThamizh picture GThamizh · Apr 15, 2015 · Viewed 80.1k times · Source
a = {'a', 'b', 'c'} 
b = {'d', 'e', 'f'}

I want to add above two set values. I need output like

c = {'a', 'b', 'c', 'd', 'e', 'f'}

Answer

TheBlackCat picture TheBlackCat · Apr 15, 2015

All you have to do to combine them is

c = a | b

Sets are unordered sequences of unique values. a | b or a.union(b) is the union of the two sets (a new set with all values found in either set). This is a class of operation called a "set operation", which Python sets provide convenient tools for.