Python dicts are now ordered
451–457 of 457 posts
Re: Python dicts are now ordered
#452Earlier quoted context omitted.
Only if you're using version specific features.
What happens when python 3.8 comes out? Everybody needs to go into your script to change the hashbang every time a new release comes?
If your code won’t work for older versions you can make an explicit check that the version of python is greater than whatever you need.
Re: Python dicts are now ordered
#453Earlier quoted context omitted.
You don't actually remove the entry, you just mark it as deleted. Eventually if too many things are deleted you repack the array. Still amortized O(1). (No different than a hash table in general, which will need to recopy the underlying array when it grows.)
Brilliant: Now your map contains an allocator and a garbage collector.
Re: Python dicts are now ordered
#454Re: Python dicts are now ordered
#455I'd love to know more about where an ordered dict comes in handy. Anyone have use cases? Otherwise, guaranteeing this behavior just seems ‾\_(ツ)_/‾ If this is useful, I wonder if an ordered set is useful.
> If this is useful, I wonder if an ordered set is useful. An ordered set is a unique list. It's useful. However it's not present in Python, dicts and sets have separate implementations and sets were not moved over to the ordered implementation (because the ordering was initially a side-effect of a change in implementation which was not considered useful or advantageous for sets).
Re: Python dicts are now ordered
#456Earlier quoted context omitted.
> PHP really got this one right (and immediately messed it up by mixing it with arrays, but hey, PHP). No, it didnt. What PHP calls "array" is actually an Ordered Map, as you alluded to: https://yaml.org/type/omap it literally says that in the documentation, paragraph one, sentence one: > An array in PHP is actually an ordered map. https://php.net/types.array The issue, if one exists, is that PHP doesnt have a sequen…
Sure, PHP arrays are actually ordered maps, but it's super confusing when used as arrays. I mean, look at code like this: $arr = []; $arr[2] = "two"; $arr[0] = "zero"; $arr[1] = "one"; echo join($arr, ", "); // two, zero, one In any other language, the result of similar code would be "zero, one, two". You'd need to `ksort` this thing to get it to behave like a normal bog-standard boring array. Everybody's writing PHP…
Re: Python dicts are now ordered
#457Earlier quoted context omitted.
I'm not clear on how this break existing code. Code that assumed it was arbitrary, would expect to handle any arbitrary order, including a happens-to-be sorted order. Code that assumed it was random, like actually inserted by random(), was already broken, because that simply isn't the case. Code that assumed the order would stay constant was relying on implementation-specific behavior, and could potentially break on…
It doesn’t break existing code. Code written for Python 3.7 might break on older versions of Python
I get the whole principal of least surprise, but not at the expense of progress.