For Python 3:
my_dict2 = {y: x for x, y in my_dict.items()}
For Python 2, you can use
my_dict2 = dict((y, x) for x, y in my_dict.iteritems())
Related Contents:
- Return a default value if a dictionary key is not available
- Dictionary: Get list of values for list of keys
- How to get a random value from dictionary?
- How to add multiple values to a dictionary key in python?
- Merging two CSV files using Python
- How to add multiple values to a dictionary key?
- How to print a dictionary’s key?
- Accessing dict_keys element by index in Python3
- How can I get a random key-value pair from a dictionary?
- Why must dictionary keys be immutable?
- How can I get dictionary key as variable directly in Python (not by searching from value)?
- How do I sort a list of dictionaries by a value of the dictionary?
- Access nested dictionary items via a list of keys?
- Create a dictionary with comprehension
- Convert list of dictionaries to a pandas DataFrame
- Flatten nested dictionaries, compressing keys
- What is the best way to implement nested dictionaries?
- How to “perfectly” override a dict?
- Getting a list of values from a list of dicts
- Access a particular field in arbitrarily nested JSON data [duplicate]
- How to return dictionary keys as a list in Python?
- Merge and sum of two dictionaries
- How to convert a file into a dictionary?
- How to save a dictionary to a file?
- Python dictionary keys. “In” complexity
- How to print a dictionary line by line in Python?
- Convert a python dict to a string and back
- Is there a difference between using a dict literal and a dict constructor?
- Python Dictionary to CSV
- Accessing value inside nested dictionaries [duplicate]
- Send some keys to inactive window with python
- Is it possible to assign the same value to multiple keys in a dict object at once?
- How can I update an attribute created by a base class’ mutable default argument, without modifying that argument? [duplicate]
- OrderedDict vs defaultdict vs dict [closed]
- Python: Using vars() to assign a string to a variable
- Can PyYAML dump dict items in non-alphabetical order?
- Merge several Python dictionaries [duplicate]
- Extract a subset of key-value pairs from dictionary?
- How to sum all the values in a dictionary?
- How to sort IP addresses stored in dictionary in Python?
- Setting a value in a nested Python dictionary given a list of indices and value
- How to json_normalize a column with NaNs
- How to create a dictionary of two pandas DataFrame columns
- Creating a new dictionary in Python
- Why is a class __dict__ a mappingproxy?
- Python: Collections.Counter vs defaultdict(int)
- Changing marker style in scatter plot according to third variable
- How do I merge dictionaries together in Python?
- Accepting a dictionary as an argument with argparse and python [duplicate]
- Comparing 2 lists consisting of dictionaries with unique keys in python