Why don't Python sets preserve insertion order?
问题 I was surprised to discover recently that while dicts are guaranteed to preserve insertion order in Python 3.7+, sets are not: >>> d = {'a': 1, 'b': 2, 'c': 3} >>> d {'a': 1, 'b': 2, 'c': 3} >>> d['d'] = 4 >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> s = {'a', 'b', 'c'} >>> s {'b', 'a', 'c'} >>> s.add('d') >>> s {'d', 'b', 'a', 'c'} What is the rationale for this difference? Do the same efficiency improvements that led the Python team to change the dict implementation not apply to sets as well?