>>> list(dict.fromkeys([3, 2, 1]))
[3, 2, 1]
>>> list(set([3, 2, 1]))
[1, 2, 3] >>> list(dict.fromkeys([3, 2, 1]))
[3, 2, 1]
>>> list(set([3, 2, 1]))
[1, 2, 3]That's why there's OrderredDict.
Unless the language explicitly says that dicts hold their order, even if they do, it's an implementation detail, and it should not be relied upon.
(Dicts in 3.7 and ordered dicts)
https://stackoverflow.com/questions/50872498/will-ordereddic...
This talk is definitely worth a watch yes :)
> What I took from that is the implementation does preserve order but it isn't guaranteed yet and in theory it could change.
This was the case for python 3.6, but starting from Python 3.7 ordering is guaranteed [1]:
Changed in version 3.7: Dictionary order is guaranteed to be insertion order.
[1] https://docs.python.org/3.7/library/stdtypes.html#dict-viewsHis talks are good, except that every time he asks a stupid rhetorical question of the audience I’m overwhelmed with the urge to wang¹ a tomato at him.
The reason being that the ordered dict ends up being faster than non-ordered, and people will rely on this implementation detail, so they added it to the spec to make that okay.
[1] https://docs.python.org/2/tutorial/datastructures.html#dicti...
Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in
insertion order ...EDIT: clicking through to GitHub, the date is clearly not up-to-date, so this might have been added later.