sort() doesn't work on dist.keys() ?

Discussion in 'Python' started by Steve Pinard, Jul 10, 2003.

  1. Steve Pinard

    Steve Pinard Guest

    (Got a comm error trying to post first time, sorry if this
    is a duplicate)

    New to Python, so please bear with me.

    >>> import sys
    >>> print sys.modules.keys() # works fine

    ['code', ...snip... ]
    >>> print sys.modules.keys().sort() # returns None, why?

    None

    According to my reference (Nutshell), keys() returns a
    "copy" of the dict keys as a list, so I would expect when
    I aply sort() to that list, I would get an in-place sorted
    version of that list. Why do I get None?

    TIA,
    - Steve
     
    Steve Pinard, Jul 10, 2003
    #1
    1. Advertising

  2. Steve Pinard

    Greg Krohn Guest

    "Steve Pinard" <> wrote in message
    news:...
    > (Got a comm error trying to post first time, sorry if this
    > is a duplicate)
    >
    > New to Python, so please bear with me.
    >
    > >>> import sys
    > >>> print sys.modules.keys() # works fine

    > ['code', ...snip... ]
    > >>> print sys.modules.keys().sort() # returns None, why?

    > None
    >
    > According to my reference (Nutshell), keys() returns a
    > "copy" of the dict keys as a list, so I would expect when
    > I aply sort() to that list, I would get an in-place sorted
    > version of that list. Why do I get None?
    >
    > TIA,
    > - Steve
    >


    You said it yourself. It's an IN PLACE sort, so it won't return anything
    (well, None, but that doesn't count). Try this:

    **UNTESTED**
    >>> mykeys = sys.modules.keys()
    >>> print mykeys

    [...a list of keys...]
    >>> mykeys.sort() #Note it's IN PLACE. Nothing is returned, so there's no

    need to assign anything
    >>> print mykeys

    [...a SORTED list of keys...]

    Greg
     
    Greg Krohn, Jul 10, 2003
    #2
    1. Advertising

  3. Steve Pinard

    Sean Ross Guest

    Sean Ross, Jul 10, 2003
    #3
  4. In article <>, Steve Pinard wrote:
    > (Got a comm error trying to post first time, sorry if this
    > is a duplicate)
    >
    > New to Python, so please bear with me.
    >
    >>>> import sys
    >>>> print sys.modules.keys() # works fine

    > ['code', ...snip... ]
    >>>> print sys.modules.keys().sort() # returns None, why?

    > None
    >
    > According to my reference (Nutshell), keys() returns a
    > "copy" of the dict keys as a list, so I would expect when
    > I aply sort() to that list, I would get an in-place sorted
    > version of that list. Why do I get None?


    From the documentation of the mutable sequence sort() method, note (7):

    "The sort() and reverse() methods modify the list in place for
    economy of space when sorting or reversing a large list. To remind
    you that they operate by side effect, they don't return the sorted
    or reversed list."

    Or, in other words, sort() always returns None. If you want to sort, you
    need to bind a name to the list you want to sort, first, then call sort() on
    it and then print the now sorted list. For example:

    sys_keys = sys.modules.keys()
    sys_keys.sort()
    print sys_keys

    Hope this helps,

    Troels Therkelsen
     
    Troels Therkelsen, Jul 10, 2003
    #4
  5. Steve Pinard

    Cliff Wells Guest

    On Thu, 2003-07-10 at 13:14, Steve Pinard wrote:
    > (Got a comm error trying to post first time, sorry if this
    > is a duplicate)
    >
    > New to Python, so please bear with me.
    >
    > >>> import sys
    > >>> print sys.modules.keys() # works fine

    > ['code', ...snip... ]
    > >>> print sys.modules.keys().sort() # returns None, why?

    > None
    >
    > According to my reference (Nutshell), keys() returns a
    > "copy" of the dict keys as a list, so I would expect when
    > I aply sort() to that list, I would get an in-place sorted
    > version of that list. Why do I get None?


    Because sort() sorts the list in-place. It doesn't return anything
    (which in Python means it returns None).

    Try this instead:

    keys = sys.modules.keys()
    keys.sort()
    print keys

    --
    Cliff Wells, Software Engineer
    Logiplex Corporation (www.logiplex.net)
    (503) 978-6726 (800) 735-0555
     
    Cliff Wells, Jul 10, 2003
    #5
  6. Steve Pinard

    Greg Fortune Guest

    Because sorts works in place and the sort fuction returns None rather than a
    copy of the list. Try


    import sys
    temp = sys.modules.keys()
    temp.sort()
    print temp


    Greg


    Steve Pinard wrote:

    > (Got a comm error trying to post first time, sorry if this
    > is a duplicate)
    >
    > New to Python, so please bear with me.
    >
    >>>> import sys
    >>>> print sys.modules.keys() # works fine

    > ['code', ...snip... ]
    >>>> print sys.modules.keys().sort() # returns None, why?

    > None
    >
    > According to my reference (Nutshell), keys() returns a
    > "copy" of the dict keys as a list, so I would expect when
    > I aply sort() to that list, I would get an in-place sorted
    > version of that list. Why do I get None?
    >
    > TIA,
    > - Steve
     
    Greg Fortune, Jul 11, 2003
    #6
    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. MS
    Replies:
    3
    Views:
    1,571
  2. Christopher Barker

    Can I use dist-utils to install a sub-package?

    Christopher Barker, May 13, 2004, in forum: Python
    Replies:
    2
    Views:
    336
    Jorge Godoy
    May 13, 2004
  3. Anastasios Hatzis

    distutils: different names in src and dist/build

    Anastasios Hatzis, Feb 8, 2007, in forum: Python
    Replies:
    1
    Views:
    274
    Richard Jones
    Feb 8, 2007
  4. Replies:
    20
    Views:
    11,542
  5. Navin
    Replies:
    1
    Views:
    727
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page