Adding an element to the key's value in ordered dictionary
<p>I have an ordered dictionary:</p>

<pre><code>{'20140106': '82.1000000',
 '20140217': '77.0300000',
 '20140224': '69.6200000',
 '20140310': '46.3300000',
 '20140414': '49.3800000',
</code></pre>

<p>Keys are dates: yyyy-mm-dd and i want to store all values of one month in one key:</p>

<pre><code>{'20140106': '82.1000000',
 '20140217': ['77.0300000','69.6200000',]
 '20140310': '46.3300000',
 '20140414': '49.3800000',
</code></pre>

<p>I realized this not very beutiful solution:</p>

<pre><code>val = []   #list, where i put all values for an exact month
previus_key = list(collection.keys())[0]
val.append(collection.get(previus_key))

for key in list(collection.keys())[1:]:
    if (key[4:6]==previus_key[4:6]):    #if it's the same month
        val.append(list(collection.pop(key)))             #remember the value and delete an item
    else:
        collection.update({previus_key:val})   #that means, we jumped to another month, so lets update the values for previus month 
        previus_key = key            #now, start the same algorihtm for a new month.
    val = collection.get(previus_key)
</code></pre>

<p>But, i get an error:<code>'str' object has no attribute 'append'</code> for line 
<code>val.append(list(collection.pop(key)))</code>. I researched it, and came to conclusion, that it must not be here! Because, i append a list to a list! 
So, please, point to my mistake and give advice, how could i make the code more beutiful. Thanks!</p>

<p>I don't see any advantage of using strings here as keys to represent the <em>date.</em> This is my solution, but I hope it won't break your current code. </p>

<p>Well, your code shows you're doing so much nesting there: </p>

<pre><code> val.append(list(collection.pop(key)))
</code></pre>

<p>And: </p>

<blockquote>
  <p>Keys are dates: yyyy-mm-dd and i want to store all values of one month in one key:</p>
</blockquote>

<p>Here's a way to do it: 
Use tuples for this purpose: </p>

<pre><code>mydict = {(year, month, day): value}
</code></pre>

<p>this way not only you're simplifying the problem, but you're also making your code more readable: </p>

<pre><code>for date in mydict: 
    year  = date[0] 
    month = date[1] 
         ...
    ...process data...
</code></pre>

<p>Though keys can only be immutable objects, hence tuples are immutable objects. </p>

python list dictionary