Input dictionary
{11: [1, 2], 23: 'ewewe', 3: [4], 41: 5, 55: 6}
I need to form another dictionary based on types of items in input dictionary, it would be like :-
{type: list of keys which has this type}
Expected output would be
{<type 'list'>: [11, 3], <type 'str'>: [23], <type 'int'>: [41, 55]}
I have written below code for this:-
input_dict = {1: [1, 2], 2: 'ewewe', 3: [4], 4: 5, 5: 6}
>>> d = {}
>>> 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]
>>> d
{<type 'list'>: [1, 3], <type 'str'>: [2], <type 'int'>: [4, 5]}
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...