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

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).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)