[Tutor] Dictionary viceversa

Zachary Ware zachary.ware+pytut at gmail.com
Mon Jul 30 14:11:43 EDT 2018


On Mon, Jul 30, 2018 at 1:08 PM Alan Gauld via Tutor <tutor at python.org> wrote:
> There are lots of options including those suggested elsewhere.
> Another involves using get() which makes your function
> look like:
>
> def viceversa(d):
>     new_d = dict()
>     for k in d:
>         for e in d[k]:
>             new_d[e] = new_d.get(e,[]).append(k)

Note that this will set each entry to `None` as returned by `list.append`.

-- 
Zach


More information about the Tutor mailing list