mapping

How do I exchange keys with values in a dictionary?

How do I exchange keys with values in a dictionary? Question: I receive a dictionary as input, and would like to to return a dictionary whose keys will be the input’s values and whose value will be the corresponding input keys. Values are unique. For example, say my input is: a = dict() a[‘one’]=1 a[‘two’]=2 …

Total answers: 19

What is the best way to implement nested dictionaries?

What is the best way to implement nested dictionaries? Question: I have a data structure which essentially amounts to a nested dictionary. Let’s say it looks like this: {‘new jersey’: {‘mercer county’: {‘plumbers’: 3, ‘programmers’: 81}, ‘middlesex county’: {‘programmers’: 81, ‘salesmen’: 62}}, ‘new york’: {‘queens county’: {‘plumbers’: 9, ‘salesmen’: 36}}} Now, maintaining and creating this …

Total answers: 21

Reverse / invert a dictionary mapping

Reverse / invert a dictionary mapping Question: Given a dictionary like so: my_map = {‘a’: 1, ‘b’: 2} How can one invert this map to get: inv_map = {1: ‘a’, 2: ‘b’} Asked By: Brian M. Hunt || Source Answers: Try this: inv_map = dict(zip(my_map.values(), my_map.keys())) (Note that the Python docs on dictionary views explicitly …

Total answers: 32