Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Dictionary : items()

Reply
Thread Tools

Dictionary : items()

 
 
koranthala
Guest
Posts: n/a
 
      01-22-2009
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?
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      01-22-2009
koranthala wrote:
> 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?


In Python 3, the current .keys() returning a list and .iterkeys()
returning an iterator both disappear and are replaced by .keys()
returning an iterable set-like view of the dict. 'sorted(d.keys())' is
the way to convert the view into a sorted list. So your idea is obsolete.

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-22-2009
On Wed, 21 Jan 2009 23:02:11 -0800, koranthala wrote:

> 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]))



That is better written as:

l = sorted(abcd.items(), key=lambda xx[1].lower(), x[0]))

where abcd is *any* kind of mapping with an items() method. It could be a
dict, a defaultdict, ordereddict, binarytree, or anything else the caller
needs.


> 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?



No.



--
Steven
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-22-2009
Steven D'Aprano <(E-Mail Removed)> writes:
> That is better written as:
> l = sorted(abcd.items(), key=lambda xx[1].lower(), x[0]))


In Python 2.x, I prefer the style

l = sorted(abcd.iteritems(), key=lambda (k,v): (v.lower(), k))

but Python 3.0 breaks the tuple unpacking per some PEP.
 
Reply With Quote
 
Benjamin
Guest
Posts: n/a
 
      01-23-2009
On Jan 22, 2:53*am, Paul Rubin <http://(E-Mail Removed)> wrote:
> Steven D'Aprano <(E-Mail Removed)> writes:
> > That is better written as:
> > l = sorted(abcd.items(), key=lambda xx[1].lower(), x[0]))

>
> In Python 2.x, I prefer the style
>
> * l = sorted(abcd.iteritems(), key=lambda (k,v): (v.lower(), k))
>
> but Python 3.0 breaks the tuple unpacking per some PEP.


PEP 3113 if you really care.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary within dictionary Fox ASP General 5 03-13-2005 05:03 AM



Advertisments