K
koranthala
Hi,
Dictionary has the items method which returns the value as a list
of tuples.
I was wondering whether it would be a good idea to have an extra
parameter - sort - to allow the tuples to be sorted as the desire of
users.
Currently what I do is:
class SDict(dict):
def items(self, sort=None):
'''Returns list. Difference from basic dict in that it is
sortable'''
if not sort:
return super(SDict, self).items()
return sorted(self.iteritems(), key=sort)
Usage:
for a dictionary of strings sorted:
l = abcd.items(sort=lambda xx[1].lower(), x[0]))
Now what I wanted was to incorporate this in the basic dictionary
itself. Not only items(), but the methods similar to it - iteritems
etc all can also have this parameter.
Please let me know your views.
Is this a good enough idea to be added to the next version of Python?
Dictionary has the items method which returns the value as a list
of tuples.
I was wondering whether it would be a good idea to have an extra
parameter - sort - to allow the tuples to be sorted as the desire of
users.
Currently what I do is:
class SDict(dict):
def items(self, sort=None):
'''Returns list. Difference from basic dict in that it is
sortable'''
if not sort:
return super(SDict, self).items()
return sorted(self.iteritems(), key=sort)
Usage:
for a dictionary of strings sorted:
l = abcd.items(sort=lambda xx[1].lower(), x[0]))
Now what I wanted was to incorporate this in the basic dictionary
itself. Not only items(), but the methods similar to it - iteritems
etc all can also have this parameter.
Please let me know your views.
Is this a good enough idea to be added to the next version of Python?