Ogen
Ogen

Reputation: 6709

Format of a permutation of a python dictionary

Hi I have this code...

x = {'stack': ['2', '3'], 'overflow': ['1', '2']}
for i in x.values():
    heroes = {x[0]:x[1:] for x in permutations(i)}
    print heroes

This gives me...

{'3': ('2',), '2': ('3',)}
{'1': ('2',), '2': ('1',)}

Later on in my program I need the values of the keys to be in list form, not tuple form. So the result I need is this...

{'3': ['2'], '2': ['3']}
{'1': ['2'], '2': ['1']}

How can I modify my code to give me this result while maintaining efficieny? Thanks a bunch.

Upvotes: 1

Views: 259

Answers (1)

John La Rooy
John La Rooy

Reputation: 304463

heroes = {x[0]:list(x[1:]) for x in permutations(i)}

Upvotes: 3

Related Questions