title: Python: sort specific pairs of dictionaries in a list of dictionaries based on occurrence of value
body: <p><strong>example:</strong></p>
  
  <pre><code>[{type:"fjsjo"}, {type:"cccc"}, {type:"xxxx"}, {type:"oooo"}]
  </code></pre>
  
  <p>results in </p>
  
  <pre><code>[{type:"fjsjo"}, {type:"xxxx"}, {type:"cccc"}, {type:"oooo"}]
  </code></pre>
  
  <p>I always need "xxxx" dictionary to come right before "cccc". How can I transform the dictionary so that when it detects the occurence of dictionaries containing values "cccc" and "xxxx" right next to each other or maybe there's another dictionary between those two, can be transformed so that "xxxx" is always ahead of "cccc" ?</p>
  
  <p><strong>example:</strong></p>
  
  <pre><code>[{type:"fjsjo"}, {type:"cccc"}, {type:"oooo"} , {type:"xxxx"}, {type:"oooo"}]
  </code></pre>
  
  <p>results in </p>
  
  <pre><code>[{type:"fjsjo"}, {type:"xxxx"}, {type:"oooo"} , {type:"cccc"}, {type:"oooo"}]
  </code></pre>
  
  <p>Also if there's two sets or more than one of the same type, then, it should always sort the dictionaries closest to the left only. </p>
  
  <p>so</p>
  
  <p><strong>example:</strong></p>
  
  <pre><code>[{type:"cccc"}, {type:"xxxx"} , {type:"cccc"}, {type:"xxxx"}]
  </code></pre>
  
  <p>results in </p>
  
  <pre><code>[{type:"xxxx"}, {type:"cccc"} , {type:"xxxx"}, {type:"cccc"}]
  </code></pre>
  
  <p>whenever the occurence of "cccc" comes before "xxxx", those two dictionaries must switch places. if they have already switched places, then they cannot be switched again. So the middle two dictionaries won't switch.</p>
  
  <p><strong>example:</strong></p>
  
  <pre><code>[{type:"cccc"}, {type:"xxxx"} , {type:"cccc"}]
  </code></pre>
  
  <p>results in </p>
  
  <pre><code>[{type:"xxxx"}, {type:"cccc"} , {type:"cccc"}]
  </code></pre>
  
  <p><strong>example:</strong></p>
  
  <pre><code>[{type:"cccc"}, {type:"oooo"} , {type:"xxxx"}, {type:"cccc"},{type:"xxxx"}]
  </code></pre>
  
  <p>results in </p>
  
  <pre><code>[{type:"xxxx"}, {type:"oooo"} , {type:"cccc"}, {type:"xxxx"},{type:"cccc"},{type:"cccc"}]
  </code></pre>