-1

Running d = [{1,2,3,4}, {2,3,4}, {3,4,5,6,7}, {4,7,9}] set.intersection(*[x for x in d]) gives '4' but running set.intersection([x for x in d]) only gives error message saying "TypeError: descriptor 'intersection' requires a 'set' object but received a 'list'". What is the use of '*' here ?

MItrajyoti
  • 508
  • 9
  • 14
  • `intersection` is not a class method. It's an instance method, so it expects its first argument to be a set, whether you use `s.intersection(...)` or `set.intersection(s, ...)`. (The *other* arguments can be arbitrary iterables of hashable values.) – chepner Jun 26 '21 at 14:24

1 Answers1

2

It serves to unpack a collection into several isolated values.

For example, in your code, it will do the same as set.intersection({1,2,3,4}, {2,3,4}, {3,4,5,6,7}, {4,7,9}), with 4 arguments.

Note that it is not necessary to have a list comprehension inside, set.intersection(*d) is equivalent.

Resources:
What does the star and doublestar operator mean in a function call? https://www.tutorialspoint.com/how-to-unpack-using-star-expression-in-python

Trevis
  • 568
  • 3
  • 10
  • Which category is this explanation is falling under as shown here : https://www.tutorialspoint.com/What-does-the-Star-operator-mean-in-Python or here: https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters – MItrajyoti Jun 26 '21 at 15:49