Dictionary : items()

Discussion in 'Python' started by koranthala, Jan 22, 2009.

  1. koranthala

    koranthala Guest

    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 x:(x[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?
     
    koranthala, Jan 22, 2009
    #1
    1. Advertising

  2. koranthala

    Terry Reedy Guest

    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 x:(x[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.
     
    Terry Reedy, Jan 22, 2009
    #2
    1. Advertising

  3. 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 x:(x[1].lower(), x[0]))



    That is better written as:

    l = sorted(abcd.items(), key=lambda x:(x[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
     
    Steven D'Aprano, Jan 22, 2009
    #3
  4. koranthala

    Paul Rubin Guest

    Steven D'Aprano <> writes:
    > That is better written as:
    > l = sorted(abcd.items(), key=lambda x:(x[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.
     
    Paul Rubin, Jan 22, 2009
    #4
  5. koranthala

    Benjamin Guest

    On Jan 22, 2:53 am, Paul Rubin <http://> wrote:
    > Steven D'Aprano <> writes:
    > > That is better written as:
    > > l = sorted(abcd.items(), key=lambda x:(x[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.
     
    Benjamin, Jan 23, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    444
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    328
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,071
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    529
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    193
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page