Python create list from smallest number of repeating values in a nested list
<p>I have a Dict</p>

<pre><code>whs = {
    'ID1' : ['code1', 'code2', 'code3'],
    'ID2' : ['code2', 'code5', 'code3'],
    'ID3' : ['code6', 'code7', 'code8'],
    'ID4' : ['code3', 'code5', 'code6'],
}
</code></pre>

<p>What I need to do is build a new list that will look like</p>

<pre><code>submit = [
    {
        'codes' : ['code3', ],
        'ids' : ['ID1', 'ID2', 'ID4'],
    },
    {
        'codes' : ['code6', 'code7', 'code8'],
        'ids' : ['ID3', ],
    }
]
</code></pre>

<p>What I have so far</p>

<pre><code>def ParseAvailable(self, whs):
    separate = whs.keys()
    submit = []
    while len(separate) &gt; 0:
        avail = {
            'codes' : [],
            'ids' : [],
        }
        for num, item in enumerate(separate):
            if len(avail['codes']) == 0:
                avail['codes'] = whs[item]
                avail['ids'].append(item)
            else:
                avail_all = list(set(avail['codes']) &amp; set(whs[item]))
                print '%s : %s' % (item, avail_all)
                if len(avail_all) &gt; 0:
                    avail['codes'] = avail_all
                    avail['ids'].append(item)
            if len(avail['codes']) &gt; 0:
                del separate[num]
        submit.append(avail)
    return submit
</code></pre>

<p>Which returns:</p>

<pre><code>[
    {
        'ids': ['ID4', 'ID3'], 
        'codes': ['code6']
    },
    {
        'ids': ['ID2'], 
        'codes': ['code2', 'code5', 'code3']
    },
    {
        'ids': ['ID1'],
         'codes': ['code1', 'code2', 'code3']
    }
]
</code></pre>

<p>which COULD work except that ID1 &amp; ID2 should be combined as</p>

<pre><code>{
    'ids' : ['ID1', 'ID2',],
    'codes' : ['code2', 'code3', ]
}
</code></pre>

<p>Curious if there is an easier approach that I've not thought of, figure I could setup a couple more nested loops to compare everything piece by piece though it seems rather unpythonic</p>

<p>Thank You in Advance</p>
