title: Python: How to sort list of dictionaries by value and index
body: <p>I have a list of a dictionary of data that is in order in some places and out of order in others: </p>
  
  <p>Eg: </p>
  
  <pre><code>data = [{"text":'a', "value":1},
          {"text":'b', "value":1},
          {"text":'j', "value":2},
          {"text":'k', "value":50},
          {"text":'b', "value":50},
          {"text":'y', "value":52},
          {"text":'x', "value":2},
          {"text":'k', "value":3},
          {"text":'m', "value":3}]
  </code></pre>
  
  <p>I want to sort them as:</p>
  
  <pre><code> o = [{"text":'a', "value":1},
        {"text":'b', "value":1},
        {"text":'j', "value":2},
        {"text":'x', "value":2},
        {"text":'k', "value":3},
        {"text":'m', "value":3},
        {"text":'k', "value":50},
        {"text":'b', "value":50},
        {"text":'y', "value":52}]
  </code></pre>
  
  <p>wherein my sorting is some combination of the index of the item and the 2nd value, I was thinking sort with:</p>
  
  <pre><code>key=[(2nd value)&lt;&lt;len(closest power of 2 to len(index)) + index]
  </code></pre>
  
  <p>I can sort by the list of dicts by the 2nd value with:</p>
  
  <pre><code>data.sort(key= lambda x:x['value'])
  </code></pre>
  
  <p>How do I also add the index of the dictionary?</p>
  
  <p>And is there a better sorting key I could use?</p>