pythonsetset-operations

Difference between union() and update() in sets, and others?


Python sets have these methods:

s.union(t)  s | t   new set with elements from both s and t

s.update(t) s |= t  return set s with elements added from t

Likewise, there's also these:

s.intersection_update(t)    s &= t  return set s keeping only elements also found in t

s.intersection(t)   s & t   new set with elements common to s and t

And so on, for all the standard relational algebra operations.

What exactly is the difference here? I see that it says that the update() versions returns s instead of a new set, but if I write x = s.update(t), does that means that id(x) == id(s)? Are they references to the same object now?

Why are both sets of methods implemented? It doesn't seem to add any significant functionality.


Solution

  • They are very different. One set changes the set in place, while the other leaves the original set alone, and returns a copy instead.

    >>> s = {1, 2, 3}
    >>> news = s | {4}
    >>> s
    set([1, 2, 3])
    >>> news
    set([1, 2, 3, 4])
    

    Note how s has remained unchanged.

    >>> s.update({4})
    >>> s
    set([1, 2, 3, 4])
    

    Now I've changed s itself. Note also that .update() didn't appear to return anything; it did not return s to the caller and the Python interpreter did not echo a value.

    Methods that change objects in-place never return the original in Python. Their return value is always None instead (which is never echoed).