Why is '+' not understood by Python sets?
I would like to know why this is valid:
set(range(10)) - set(range(5))
but this is not valid:
set(range(10)) + set(range(5))
Is it because '+' could mean both intersection and union?
Solution 1:
Python sets don't have an implementation for the +
operator.
You can use |
for set union and &
for set intersection.
Sets do implement -
as set difference. You can also use ^
for symmetric set difference (i.e., it will return a new set with only the objects that appear in one set but do not appear in both sets).
Solution 2:
Python chose to use |
instead of +
because set union is a concept that is closely related to boolean disjunction; Bit vectors (which in python are just int
/long
) define this operation across a sequence of boolean values and call it "bitwise or". In fact this operation is so similar to the set union that binary integers are sometimes also called "Bit sets", where the elements in the set are taken to be the natural numbers.
Because int
already defines set-like operators as |
, &
and ^
, it was natural for the newer set
type to use the same interface.