How to map dict into another dict Python

You can use a defaultdict as follow:

class Account:
    def __init__(self, x, y):
        self.x = x
        self.y = y

d_in = {0: Account(13, 1), 1: Account(15, 5), 2: Account(55, 1), 3: Account(33 ,1)} 
d_out = collections.defaultdict(list)

for index, k in enumerate(d_in.keys()):
    d_out[d_in[k].y].append(index)

print d_out

Giving the following output:

defaultdict(<type 'list'>, {1: [0, 2, 3], 5: [1]})

If you really want it to look like a regular dict then:

print dict(d_out)

Giving:

{1: [0, 2, 3], 5: [1]}

Leave a Comment