Reputation: 113
I was wondering whether there is a difference between using "&" and ".intersection()" while finding the intersection of two sets in Python. If so, what are they?
Thank you.
Upvotes: 0
Views: 373
Reputation: 11651
You can read the official documentation. Here is the main difference (emphasis is mine) :
Note, the non-operator versions of union(), intersection(), difference(), and symmetric_difference(), issubset(), and issuperset() methods will accept any iterable as an argument. In contrast, their operator based counterparts require their arguments to be sets. This precludes error-prone constructions like set('abc') & 'cbs' in favor of the more readable set('abc').intersection('cbs').
Using the non operator function does not require to convert the iterable as a set.
Upvotes: 2