Creating an empty set

后端 未结 4 2053
刺人心
刺人心 2021-01-03 23:15

I have some code which tots up a set of selected values. I would like to define an empty set and add to it, but {} keeps turning into a dictionary. I have foun

4条回答
  •  借酒劲吻你
    2021-01-03 23:35

    A set literal is just a tuple of values in curly braces:

    x = {2, 3, 5, 7}
    

    So, you can create an empty set with empty tuple of values in curly braces:

    x = {*()}
    

    Still, just because you can, doesn't mean you should.

    Unless it's an obfuscated programming, or a codegolf where every character matters, I'd suggest an explicit x = set() instead.

    "Explicit is better than implicit."

提交回复
热议问题