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>You can use an <code>OrderedDict</code></p>

<p>Just a simple change would resolve your problem </p>

<pre><code>ord_dic = OrderedDict(sorted(dic.items(), key=lambda t: str_in.index(t[0])))
</code></pre>

<p>So you could use something like </p>

<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
ord_dic = OrderedDict(sorted(dic.items(), key=lambda t: str_in.index(t[0])))
for key in ord_dic:
    if key == max(ord_dic, key=ord_dic.get):
        return key
        break
</code></pre>

<p>Do not forget to import <code>OrderedDict</code> <br/>
Add this line at the top of your code:</p>

<pre><code>from collections import OrderedDict
</code></pre>

python string dictionary