How to return the first highest value that is placed in a dictionary
<pre><code>dic = {}
count = 0
i = 0
str_in = str_in.replace(' ','')
while i &lt; len(str_in):
    count = str_in.count(str_in[i])
    dic[str_in[i]] = count
    i += 1
for key in dic:
    if key == max(dic, key=dic.get):
        return key
        break
</code></pre>

<p>The dictionary that is made in this program is</p>

<pre><code>{'i': 1, 'h': 2, 'j': 2, 'o': 2, 'n': 1, 's': 2}
</code></pre>

<p>from the input <code>'joshin josh'</code></p>

<p>I am pretty sure the dictionary's max value returns h because it is the first value in the dictionary with the highest value, even if tied.
I want it to return j because that is the first letter I put into the dictionary. It seems like the dictionary automatically sorts the letters alphabetically, which I also don't understand. I want this to work with any string and all tied letters, not just this particular string. Correct me if I am wrong on anything, I am a noob.</p>

<p>Another approach, by using <code>OrderedDict</code> as a mixin class it allows an ordered <code>Counter</code> class to be easily composed. You can then use the first entry of the list returned from the <code>most_common</code> method.</p>

<p>e.g.</p>

<pre><code>from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
    pass

oc = OrderedCounter('joshin josh')
print(oc.most_common()[0])
</code></pre>

<p>Will give you back a tuple of the letter and the number of occurrences. e.g.</p>

<blockquote>
  <p>('j', 2)</p>
</blockquote>

<p>You'd need to protect the <code>[0]</code> in the case where you were passing an empty string, Where nothing is most common.</p>

<p>To better understand why this works I recommend watching Raymond Hettingers Super Considered Super talk from PyCon 2015. Youtube link <a href="https://www.youtube.com/watch?v=EiOglTERPEo" rel="nofollow">here</a></p>

python string dictionary