Python Set: why is my_set = {*my_list} invalid?

Viewed 616

I am unable to figure out why this code doesn't work

>>> my_list = [1,2,3,4,5]
>>> my_set = {*my_list}
   File "<stdin>", line 1
    my_set = {*my_list}
          ^
SyntaxError: invalid syntax

*args is used in python to unpack list. My expectation was that the above operation would create a set but it didn't. Can *args and **kwargs in Python be only used to pass arguments as function ?

I am aware of the set() function but curious why this syntax doesn't work.

2 Answers
Related