Is there a good way to invert dictionary so key:value gets to be value:key?
bumsfeld
413
Nearly a Posting Virtuoso
Recommended Answers
Jump to PostHi!
>>> d = {"a": "one", "b": "two", "c": "three"} >>> inverted = dict([[v,k] for k,v in d.items()]) >>> inverted {'three': 'c', 'two': 'b', 'one': 'a'}
And here is a clever way I found to invert a dict where some keys have the same value:
All 4 Replies
mawe
6
Junior Poster
bumsfeld
413
Nearly a Posting Virtuoso
mawe
6
Junior Poster
bumsfeld
413
Nearly a Posting Virtuoso
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.