Difference between dict and set (python)

后端 未结 3 563
难免孤独
难免孤独 2020-12-13 08:20

So, I know that this,

a = {}  # dict

Constructs an empty dictionary. Now, I also picked up that this,

b = {1, 2, 3}  # set
         


        
3条回答
  •  借酒劲吻你
    2020-12-13 09:11

    The fact that {} is used for an empty dictionary and not for an empty set has largely historical reasons. The syntax {'a': 100, 'b': 200} for dictionaries has been around since the beginning of Python. The syntax {1, 2, 3} for sets was introduced with Python 2.7. Since {} has been used for such a long time it will stay as the way to define an empty dictionary. If Python would have had the new set syntax since the beginning, likely an empty set would be defined with {} and an empty dictionary with {:}.

提交回复
热议问题