sort a dictionary by keys in specific order

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

    #1

    sort a dictionary by keys in specific order

    hi i have a normal dictionary with key and value pairs. now i wanna
    sort by the keys BUT in a specific order i determine in a list !? any
    ideas

    dic = {'key1':'value1 ', 'key2':'value2' , 'key3':'value3' }

    list = [key2, key3, key1]

  • Paul Rubin

    #2
    Re: sort a dictionary by keys in specific order

    "spohle" <spohle@gmail.c om> writes:[color=blue]
    > dic = {'key1':'value1 ', 'key2':'value2' , 'key3':'value3' }
    >
    > list = [key2, key3, key1][/color]

    Is this what you want?

    dic = {'key1':'value1 ', 'key2':'value2' , 'key3':'value3' }
    keys = ['key2', 'key3', 'key1']
    items = [dic[k] for k in keys]
    print items

    Comment

    • Tim Chase

      #3
      Re: sort a dictionary by keys in specific order

      > hi i have a normal dictionary with key and value pairs. now i wanna[color=blue]
      > sort by the keys BUT in a specific order i determine in a list !? any
      > ideas
      >
      > dic = {'key1':'value1 ', 'key2':'value2' , 'key3':'value3' }
      >
      > list = [key2, key3, key1][/color]

      1) it's bad practice to shadow the list() command...funky stuff
      can happen.

      2) I presume your list is a list of strings, not of references:

      order = ['key2', 'key3', 'key1']

      3) As a dictionary is an unordered collection, I presume you want
      a resulting list of key/value pairs.

      If both #2 and #3 hold, you can use

      results = [(k,dic[k]) for k in order]

      which will return a list of tuples in the desired key order.

      -tkc





      Comment

      • spohle

        #4
        Re: sort a dictionary by keys in specific order

        how do i get the result back into the dictionary ?

        Comment

        • Tim Chase

          #5
          Re: sort a dictionary by keys in specific order

          > how do i get the result back into the dictionary ?

          Well, if you must, if you've just got the results in my
          previous post, you can take them and shove them back into a
          dict with

          results = [('key1','value1 '),('key2','val ue2)]
          newDict = dict(results)

          If you're not doing anything with that the resulting list of
          ordered key/value pairs (such as inserting, printing,
          whatever), then skip the whole matter:

          newDict = dic

          :)

          HOWEVER...as I noted, a dictionary is an INHERENTLY UNSORTED
          COLLECTION. There are some wrapper-classes around that will
          feign sortedness if you want them, but they may not handle
          your custom sortedness constraint.

          There are few reasons for a sorted dict. Most of them
          regard displaying them. If this is the case, just do the
          sorting/selection of the items before you print:

          print "\n".join([dic[k] for k in order])

          Other reasons might involve dependancies, where some process
          requires that you access the keys in a particular order.
          Just order them before you call the process.

          -tkc






          Comment

          • spohle

            #6
            Re: sort a dictionary by keys in specific order

            i write the dict out to a file, not with file methods but rather with
            an inhouse python code. unfortunatly the order plays a big role for
            that.

            Comment

            • Bruno Desthuilliers

              #7
              Re: sort a dictionary by keys in specific order

              Tim Chase a écrit :
              (snip)[color=blue]
              >[color=green]
              >> list = [key2, key3, key1][/color]
              >
              > 1) it's bad practice to shadow the list() command...[/color]

              s/command/type/
              [color=blue]
              > funky stuff can happen.[/color]

              indeed, if you shadow it with a non-compatible object !-)

              Comment

              • Bruno Desthuilliers

                #8
                Re: sort a dictionary by keys in specific order

                spohle a écrit :[color=blue]
                > how do i get the result back into the dictionary ?
                >[/color]
                Python dicts (like almost any known hash-table) are *not* ordered. If
                you need an ordered dict, roll your own - this is quite easy.

                Comment

                • Wolfgang Grafen

                  #9
                  Re: sort a dictionary by keys in specific order

                  spohle wrote:[color=blue]
                  > hi i have a normal dictionary with key and value pairs. now i wanna
                  > sort by the keys BUT in a specific order i determine in a list !? any
                  > ideas
                  >
                  > dic = {'key1':'value1 ', 'key2':'value2' , 'key3':'value3' }
                  >
                  > list = [key2, key3, key1]
                  >[/color]

                  You could use the seqdict package at


                  Regards

                  Wolfgang

                  Comment

                  Working...