DEV Community

loading...

Discussion on: How to Invert a Dictionary in Python: Comprehensions, Defaultdict, and More

Collapse
renegadecoder94 profile image
Jeremy Grifski Author • Edited

Great addition! I would prefer to maintain the dict type and leave out any extraneous imports, but this gets the job done and is probably less error prone.

Mind if I add it to the list?

Collapse
nielsgl profile image
Niels van Galen Last

Go ahead :)

It's part of the standard library and it's still a dict so you can use any it like a normal dict, e.g. my_dict_inverted['One for All'] as well as all methods from a normal dict like keys(), values() and items().

Thread Thread
renegadecoder94 profile image
Jeremy Grifski Author

Hi again! I'm in the process of updating this article, and I just realized that this answer has a minor problem. We're using a dictionary comprehension in a bad way. We're updating some external dictionary while the comprehension will also generate a dictionary (in this case, {None}). I believe that's considered bad practice, so I've added a note in the original article.