Re:Ask HN: C/C++ developer wanting to learn efficient Python
by timmaxw on Wednesday April 10, 2024 @12:34PM
(#39992642)
Yes, Python dicts remember insertion order. This is different from C++ std::map, which maintains the keys in sorted order. For example, std::map::lower_bound(X) finds "the smallest key in the map which is greater than or equal to X" in O(log(N)) time. I don't think Python has any data structure in the standard library that supports this operation while also supporting insertion in O(log(N)) time.
Re:Ask HN: C/C++ developer wanting to learn efficient Python
by alfons_foobar on Wednesday April 10, 2024 @12:12PM
(#39992358)
Ordered by insertion (see [Mailinglist](https://mail.python.org/pipermail/python-dev/2017-December/1...))
This might or might not be what you want/expect...
Re:Ask HN: C/C++ developer wanting to learn efficient Python
by johndough on Wednesday April 10, 2024 @12:32PM
(#39992630)
You are correct. Dicts are ordered by insertion. Also, I'd like to add that, maybe surprisingly, sets are not.
>>> set([3, 2, 1])
{1, 2, 3}
>>> set([10, 100, 1000])
{1000, 10, 100}
Re:Ask HN: C/C++ developer wanting to learn efficient Python
by az09mugen on Wednesday April 10, 2024 @12:19PM
(#39992454)
You're right, it's even python 3.6, and you have also the OrderedDict https://realpython.com/python-ordereddict/
Re:Ask HN: C/C++ developer wanting to learn efficient Python
by DangitBobby on Wednesday April 10, 2024 @12:16PM
(#39992416)
A little nitpick: they were always ordered by insertion, 3.7 just codified that behavior as a guarantee instead of an implementation detail.
I'm fairly certain that in Python 3.7 and later standard library dictionaries are now ordered by default.