Recently, I needed to find the intersection of a collection of sets, i.e. S1 ∩ S2 ∩ ... ∩ Sn in Python. I found a very interesting answer at Best way to find the intersection of multiple sets? so that my code solution was:
s1, s2, s3 = set("abc"), set("cde"), set("acf")
list_of_sets = [s1, s2, s3]
result = set.intersection(*list_of_sets) # {'c'}
I was surprised to see .intersection()
called on set
the class rather than on a set
object.
My question is where can I see documentation for this?
The above linked StackOverflow answer says
Note that set.intersection is not a static method, but this uses the functional notation to apply intersection of the first set with the rest of the list. So if the argument list is empty this will fail.
but the official docs do not seem to describe this: intersection(*others)
. Searching "python set functional notation" doesn't seem to help either. The official docs describe what I was expecting, which was to use .intersection()
on a set
object, e.g.
result = s1.intersection(*[s2, s3])
I'm also interested to know what other methods from other data structures can be used in this same way.