Python: What is the difference between set.difference and set.difference_update?

s.difference (t) returns a new set with no elements in t .

s.difference_update (t) returns the updated set without elements in t .

What is the difference between these two given methods? Because update_update sets s, what precautions should be taken to avoid getting a None result from this method?

In terms of speed, you should not set set.difference_update faster, since you only remove elements from set s instead of creating a new set, for example, in set.difference ()?

+4
source share
2

Q. ?

A.. , , . , .

Q. updates_update , , None ?

A.. Mutating Python None , . "" , "" .

Q. set.difference_update , s , , set.difference()?

A. , .

, .

s , t, , . , "s - t n = s.copy(); n.difference_update(t)). , s t

n, s n, t.

+11

difference_update , .

>>> s={1,2,3,4,5}
>>> t={3,5}
>>> s.difference(t)
{1, 2, 4}
>>> s
{1, 2, 3, 4, 5}
>>> s.difference_update(t)
>>> s
{1, 2, 4}
+4

All Articles