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>Here is a better approach. Given a dict of type {str:str}, we can turn the value into a list on inserts while retaining all the functionality of dict by extending the builtin <code>dict</code> class.</p>

<p>Here is a demonstration.</p>

<pre><code>&gt;&gt;&gt; class CustomDict(dict):
...     def insert(self, key, val): # add a new method, `insert`
...             if self.get(key):
...                     if not isinstance(self.get(key), list):
...                             lst = []
...                             lst.append(self.get(key))
...                             lst.append(val)
...                             self[key] = lst[:] # copy list
...                     else:
...                             self[key].append(val)
...             else:
...                     self[key] = val
...
&gt;&gt;&gt; vars = {'19900326': '372.99101',
...         '19730529': '291.38291',
...         '19430122': '81.248291',
...         '19930227': '192.32919',}
&gt;&gt;&gt; vars2 = CustomDict(vars)
&gt;&gt;&gt; vars2
{'19730529': '291.38291',
 '19900326': '372.99101',
 '19430122': '81.248291',
 '19930227': '192.32919'}
&gt;&gt;&gt; vars2.insert('19730529', '102.88391')
&gt;&gt;&gt; vars2
{'19730529': ['291.38291', '102.88391'],
 '19900326': '372.99101',
 '19430122': '81.248291',
 '19930227': '192.32919'}
</code></pre>

<p>In your case, you'll want to inherit from collections.ordereddict</p>

<p>As far as the logic of wanting to store all the pieces of data from the same month in the same list, you can pretty easily code that in there. Just to be sure though, it looks like you're currently disregarding year entirely. Are you sure all you care about is the same month, even if the data comes from different years? If not, you should change your logic so that you can differentiate which year.</p>

<p>Somebody suggested a better representation of the date, as a 3-tuple <code>(year, month, day)</code>. This is immutable, so it can be your dict key. (Lists are mutable and cannot be dict keys).</p>

<p>You could add the month-grouping logic to the insert method, or you could move that logic elsewhere in your code.</p>

python list dictionary