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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Pinard

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

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

    New to Python, so please bear with me.
    [color=blue][color=green][color=darkred]
    >>> import sys
    >>> print sys.modules.key s() # works fine[/color][/color][/color]
    ['code', ...snip... ][color=blue][color=green][color=darkred]
    >>> print sys.modules.key s().sort() # returns None, why?[/color][/color][/color]
    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 Krohn

    #2
    Re: sort() doesn't work on dist.keys() ?


    "Steve Pinard" <[email protected] kwell.com> wrote in message
    news:6cd58b6.03 07101214.34e99f [email protected] le.com...[color=blue]
    > (Got a comm error trying to post first time, sorry if this
    > is a duplicate)
    >
    > New to Python, so please bear with me.
    >[color=green][color=darkred]
    > >>> import sys
    > >>> print sys.modules.key s() # works fine[/color][/color]
    > ['code', ...snip... ][color=green][color=darkred]
    > >>> print sys.modules.key s().sort() # returns None, why?[/color][/color]
    > 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
    >[/color]

    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**[color=blue][color=green][color=darkred]
    >>> mykeys = sys.modules.key s()
    >>> print mykeys[/color][/color][/color]
    [...a list of keys...][color=blue][color=green][color=darkred]
    >>> mykeys.sort() #Note it's IN PLACE. Nothing is returned, so there's no[/color][/color][/color]
    need to assign anything[color=blue][color=green][color=darkred]
    >>> print mykeys[/color][/color][/color]
    [...a SORTED list of keys...]

    Greg


    Comment

    • Troels Therkelsen

      #3
      Re: sort() doesn't work on dist.keys() ?

      In article <6cd58b6.030710 1214.34e99f2a@p osting.google.c om>, Steve Pinard wrote:[color=blue]
      > (Got a comm error trying to post first time, sorry if this
      > is a duplicate)
      >
      > New to Python, so please bear with me.
      >[color=green][color=darkred]
      >>>> import sys
      >>>> print sys.modules.key s() # works fine[/color][/color]
      > ['code', ...snip... ][color=green][color=darkred]
      >>>> print sys.modules.key s().sort() # returns None, why?[/color][/color]
      > 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?[/color]

      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.key s()
      sys_keys.sort()
      print sys_keys

      Hope this helps,

      Troels Therkelsen

      Comment

      • Sean Ross

        #4
        Re: sort() doesn't work on dist.keys() ?


        "Steve Pinard" <[email protected] kwell.com> wrote in message
        news:6cd58b6.03 07101214.34e99f [email protected] le.com...[color=blue]
        > 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?[/color]

        Apparently, you get None to remind you that you've done an inplace sort,

        see



        Comment

        • Cliff Wells

          #5
          Re: sort() doesn't work on dist.keys() ?

          On Thu, 2003-07-10 at 13:14, Steve Pinard wrote:[color=blue]
          > (Got a comm error trying to post first time, sorry if this
          > is a duplicate)
          >
          > New to Python, so please bear with me.
          >[color=green][color=darkred]
          > >>> import sys
          > >>> print sys.modules.key s() # works fine[/color][/color]
          > ['code', ...snip... ][color=green][color=darkred]
          > >>> print sys.modules.key s().sort() # returns None, why?[/color][/color]
          > 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?[/color]

          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.key s()
          keys.sort()
          print keys

          --
          Cliff Wells, Software Engineer
          Logiplex Corporation (www.logiplex.net)
          (503) 978-6726 (800) 735-0555


          Comment

          • Greg Fortune

            #6
            Re: sort() doesn't work on dist.keys() ?

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


            import sys
            temp = sys.modules.key s()
            temp.sort()
            print temp


            Greg


            Steve Pinard wrote:
            [color=blue]
            > (Got a comm error trying to post first time, sorry if this
            > is a duplicate)
            >
            > New to Python, so please bear with me.
            >[color=green][color=darkred]
            >>>> import sys
            >>>> print sys.modules.key s() # works fine[/color][/color]
            > ['code', ...snip... ][color=green][color=darkred]
            >>>> print sys.modules.key s().sort() # returns None, why?[/color][/color]
            > 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[/color]

            Comment

            Working...