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

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    Python sets have these methods for all the standard relational algebra operations:

    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


    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


    What's the difference? 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.

    Continue reading...

Compartilhe esta Página