Process a dictionary based on types of it's value and generate another dictionary by using dictionary comprehension
<p>Input dictionary </p>

<pre><code>{11: [1, 2], 23: 'ewewe', 3: [4], 41: 5, 55: 6}
</code></pre>

<p>I need to form another dictionary based on types of items in input dictionary, 
it would be like :- </p>

<pre><code>{type: list of keys which has this type}
</code></pre>

<p>Expected output would be</p>

<pre><code>{&lt;type 'list'&gt;: [11, 3], &lt;type 'str'&gt;: [23], &lt;type 'int'&gt;: [41, 55]}
</code></pre>

<p>I have written below code for this:- </p>

<pre><code>input_dict = {1: [1, 2], 2: 'ewewe', 3: [4], 4: 5, 5: 6}
&gt;&gt;&gt; d = {}
&gt;&gt;&gt; seen = []
for key,val in input_dict.items():
    if type(val) in seen:
        d[type(val)].append(key)
    else:
        seen.append(type(val))
        d[type(val)] = [key]
&gt;&gt;&gt; d
{&lt;type 'list'&gt;: [1, 3], &lt;type 'str'&gt;: [2], &lt;type 'int'&gt;: [4, 5]}
</code></pre>

<p>I am trying to replace above code with dictionary comprehension, I couldn't do it after spending hours, Any help would be appreciated. Thanks in advance...</p>

<p>Using <code>defaultdict</code> and dictionary comprehension (kind of):</p>

<pre><code>from collections import defaultdict

input_dict = {1: [1, 2], 2: 'ewewe', 3: [4], 4: 5, 5: 6}

d = defaultdict(list)
{d[type(value)].append(key) for key, value in input_dict.items()}
d = dict(d)

print(d)
</code></pre>

<p><strong>Output</strong></p>

<pre><code>{&lt;type 'list'&gt;: [1, 3], &lt;type 'int'&gt;: [4, 5], &lt;type 'str'&gt;: [2]}
</code></pre>

python dictionary dictionary-comprehension