Inverse Dictionary Values List - Python Last Updated : 28 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]}Using defaultdictIn this method we use defaultdict from the collections module to efficiently reverse the key-value mapping in a dictionary. It iterates through each key-value pair thus appending the key to the corresponding list in the result. Python from collections import defaultdict d = {1: [2, 3], 2: [3], 3: [1], 4: [2, 1]} # Reverse mapping res = defaultdict(list) for k, v in d.items(): for i in v: res[i].append(k) print(dict(res)) Output{2: [1, 4], 3: [1, 2], 1: [3, 4]} Using setdefault()This method uses the setdefault() method from the dictionary to initialize the list for each key that doesn't exist in the result dictionary. It iterates through the original dictionary and appends the key to the list of each value. Python d = {1: [2, 3], 2: [3], 3: [1], 4: [2, 1]} # Reverse mapping res = {} for k, v in d.items(): for i in v: res.setdefault(i, []).append(k) print(res) Output{2: [1, 4], 3: [1, 2], 1: [3, 4]} Explanation: Outer loop processes each key-value pair and the setdefault() method ensures that each key in the result dictionary has a list initialized before appending the key to it. Comment More infoAdvertise with us Next Article Inverse Dictionary Values List - Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python dictionary-programs Practice Tags : python Similar Reads Get Index of Values in Python Dictionary Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe 3 min read Python - Assign Reversed Values in Dictionary Given a dictionary, assign each key, values after reverting the values of dictionary. Input : {1 : 4, 2 : 5, 3 : 6} Output : {1 : 6, 2 : 5, 3 : 4} Explanation : Value order changed, 4, 5, 6 to 6, 5, 4. Input : {1 : 5, 2 : 5, 3 : 5} Output : {1 : 5, 2 : 5, 3 : 5} Explanation : Same values, no visible 4 min read Reverse Dictionary Keys Order - Python We are given a dictionary and our task is to reverse the order of its keys. This means if we have a dictionary like {'a': 1, 'b': 2, 'c': 3} then the output will be {'c': 3, 'b': 2, 'a': 1}. This can be done using methods like reversed(), dictionary comprehensions, or OrderedDict. Let's explore thes 4 min read Python | Filter the negative values from given dictionary Given a dictionary, the task is to filter all the negative values from given dictionary. Let's discuss few methods to do this task. Method #1: Using dict comprehension Follow the below steps to implement: Initializing a dictionary named ini_dict with some key-value pairs.Print the initial dictionary 6 min read How to Reverse a Dictionary in Python Reversing a dictionary typically means swapping the keys and values, where the keys become the values and the values become the keys. In this article, we will explore how to reverse a dictionary in Python using a variety of methods.Using Dictionary ComprehensionDictionary comprehension is a concise 2 min read Like