[Python-ideas] Proposal: New syntax for OrderedDict, maybe built-in

Norman Hooper norman at kaapstorm.com
Thu Sep 4 19:35:06 CEST 2014


Hi there,

I'd like to propose that the OrderedDict get a more readable syntax, the 
way the syntax for set changed from set(['spam', 'ham', 'eggs']) to 
{'spam', 'ham', 'eggs'} when it became a built-in type in Python 2.4.

The way set is unordered like the keys of a dict, similarly list is 
ordered like the keys of an OrderedDict. In that sense, what {'ham', 
'eggs'} is to {'ham': 'spam', 'eggs': 'spam'}, so ['ham', 'eggs'] is to 
... my proposal of a cleaner OrderedDict syntax ... ['ham': 'spam', 
'eggs': 'spam'].

So a "dict" is like a "set" (hence curly braces) of key-value pairs. And 
an "OrderedDict" is like a "list" (hence square braces) of key-value 
pairs. (Of course, I am ignoring how lists can have duplicate items. An 
ordered set would carry the analogy further, but it wouldn't help 
illustrate my syntax proposal.)

I find "['ham': 'spam', 'eggs': 'spam']"" unambiguous, and more readable 
than "OrderedDict([('ham', 'spam'), ('eggs', 'spam')])".

I work with OrderedDict a lot, because JSON represents an OrderedDict 
and I need to work with JSON a lot. In an attempt to make the 
OrderedDicts that I work with more readable, I wrote a module to parse a 
string representation of an OrderedDict that uses my proposed syntax. 
You can find it here: https://github.com/kaapstorm/listdict

With the ubiquity of JSON, it may also be time to promote OrderedDict to 
a built-in type too.

Kind regards,

Norman



More information about the Python-ideas mailing list