Restructure dictionary (Python 2.5)

imageguy imageguy1206 at gmail.com
Mon Mar 2 10:21:23 EST 2009


> ...  this is a crappy solution that's underusing Python. Please
> enlighten me of a better way!
>
> - Fencer

One thing that springs to mind is using the dict method .setdefault

<untested>
for dkey, vallist in old_dict.iteritems():
    for val in vallist:
        new_dict.setdefault(val, set()).add(dkey)
</untested>





More information about the Python-list mailing list