How to sort this type of dictionary in python2 [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • How do I sort a dictionary by value?

    41 answers



  • How can I sort a dictionary by key?

    25 answers




deli = {'deli1': 'bbb', 'deli': 'aaa', 'deli3': 'ccc'}



How to get the dictionary sorted through key



Finally, the printing results of value are as follows.



aaa, bbb, ccc










share|improve this question













marked as duplicate by juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 10:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    0
    down vote

    favorite













    This question already has an answer here:




    • How do I sort a dictionary by value?

      41 answers



    • How can I sort a dictionary by key?

      25 answers




    deli = {'deli1': 'bbb', 'deli': 'aaa', 'deli3': 'ccc'}



    How to get the dictionary sorted through key



    Finally, the printing results of value are as follows.



    aaa, bbb, ccc










    share|improve this question













    marked as duplicate by juanpa.arrivillaga python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 12 at 10:02


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:




      • How do I sort a dictionary by value?

        41 answers



      • How can I sort a dictionary by key?

        25 answers




      deli = {'deli1': 'bbb', 'deli': 'aaa', 'deli3': 'ccc'}



      How to get the dictionary sorted through key



      Finally, the printing results of value are as follows.



      aaa, bbb, ccc










      share|improve this question














      This question already has an answer here:




      • How do I sort a dictionary by value?

        41 answers



      • How can I sort a dictionary by key?

        25 answers




      deli = {'deli1': 'bbb', 'deli': 'aaa', 'deli3': 'ccc'}



      How to get the dictionary sorted through key



      Finally, the printing results of value are as follows.



      aaa, bbb, ccc





      This question already has an answer here:




      • How do I sort a dictionary by value?

        41 answers



      • How can I sort a dictionary by key?

        25 answers








      python python-2.7






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 9:55









      Scheinin

      73




      73




      marked as duplicate by juanpa.arrivillaga python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 12 at 10:02


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by juanpa.arrivillaga python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 12 at 10:02


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          To print the values corresponding with sorted keys, just iterate over the sorted keys and lookup the value:



          for key in sorted(deli):
          print deli[key]


          would output:




          aaa
          bbb
          ccc





          share|improve this answer






























            up vote
            0
            down vote













            In Python2x, the order of the keys is not ensured. So you've to use OrderedDict if you want to maintain the order of keys in the dictionary (or a tuple of tuples).



            import collections

            sorted_deli = collections.OrderedDict()

            for key in sorted(deli):
            sorted_deli[key] = deli[key]

            print(sorted_deli.values())





            share|improve this answer




























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              To print the values corresponding with sorted keys, just iterate over the sorted keys and lookup the value:



              for key in sorted(deli):
              print deli[key]


              would output:




              aaa
              bbb
              ccc





              share|improve this answer



























                up vote
                0
                down vote



                accepted










                To print the values corresponding with sorted keys, just iterate over the sorted keys and lookup the value:



                for key in sorted(deli):
                print deli[key]


                would output:




                aaa
                bbb
                ccc





                share|improve this answer

























                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  To print the values corresponding with sorted keys, just iterate over the sorted keys and lookup the value:



                  for key in sorted(deli):
                  print deli[key]


                  would output:




                  aaa
                  bbb
                  ccc





                  share|improve this answer














                  To print the values corresponding with sorted keys, just iterate over the sorted keys and lookup the value:



                  for key in sorted(deli):
                  print deli[key]


                  would output:




                  aaa
                  bbb
                  ccc






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 at 10:03

























                  answered Nov 12 at 9:57









                  mhawke

                  58.2k75280




                  58.2k75280
























                      up vote
                      0
                      down vote













                      In Python2x, the order of the keys is not ensured. So you've to use OrderedDict if you want to maintain the order of keys in the dictionary (or a tuple of tuples).



                      import collections

                      sorted_deli = collections.OrderedDict()

                      for key in sorted(deli):
                      sorted_deli[key] = deli[key]

                      print(sorted_deli.values())





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        In Python2x, the order of the keys is not ensured. So you've to use OrderedDict if you want to maintain the order of keys in the dictionary (or a tuple of tuples).



                        import collections

                        sorted_deli = collections.OrderedDict()

                        for key in sorted(deli):
                        sorted_deli[key] = deli[key]

                        print(sorted_deli.values())





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          In Python2x, the order of the keys is not ensured. So you've to use OrderedDict if you want to maintain the order of keys in the dictionary (or a tuple of tuples).



                          import collections

                          sorted_deli = collections.OrderedDict()

                          for key in sorted(deli):
                          sorted_deli[key] = deli[key]

                          print(sorted_deli.values())





                          share|improve this answer












                          In Python2x, the order of the keys is not ensured. So you've to use OrderedDict if you want to maintain the order of keys in the dictionary (or a tuple of tuples).



                          import collections

                          sorted_deli = collections.OrderedDict()

                          for key in sorted(deli):
                          sorted_deli[key] = deli[key]

                          print(sorted_deli.values())






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 at 9:58









                          hspandher

                          8,9741025




                          8,9741025















                              Popular posts from this blog

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python