I was having some tension between a set and a list in a class and idly decided to see if sets seemed to preserve insertion order like dicts. Well, no, but I got a surprise:
Python 3.10.6 (main, Jan 1 2024, 19:58:15) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s={1,3,5,7,9}
>>> list(s)
[1, 3, 5, 7, 9]
>>> s.add(11)
>>> list(s)
[1, 3, 5, 7, 9, 11]
>>> s.add(9)
>>> list(s)
[1, 3, 5, 7, 9, 11]
>>> s.add(4)
>>> list(s)
[1, 3, 4, 5, 7, 9, 11]
>>> s.add(2)
>>> list(s)
[1, 2, 3, 4, 5, 7, 9, 11]
>>> s.add(10)
>>> s
{1, 2, 3, 4, 5, 7, 9, 10, 11}
>>> list(s)
[1, 2, 3, 4, 5, 7, 9, 10, 11]
This is a small set, but is this expected? An artifact of int
hash functions? Something else?