How to reverse a dictionary (whose values are lists) in Python?

Utsav Maniar

I would like to write a function that receives a dictionary as input argument and returns a reverse of the input dictionary where the values of the original dictionary are used as keys for the returned dictionary and the keys of the original dictionary are used as value for the returned dictionary as explained below:

dict = {'Accurate': ['exact', 'precise'], 
        'exact': ['precise'], 
        'astute': ['Smart', 'clever'], 
        'smart': ['clever', 'bright', 'talented']}

to

dict = {'precise': ['accurate', 'exact'], 
        'clever': ['astute', 'smart'], 
        'talented': ['smart'], 
        'bright': ['smart'],
        'exact': ['accurate'],
        'smart': ['astute']}

The list of values in the returned dictionary should be sorted in ascending order. Capitalization does not matter. This means that all the words should be converted to lower case letters. For example the word "Accurate" is capitalized in the original dictionary but in the returned dictionary it is written with all lower case letters.

#My code is:
from collections import defaultdict
def reverse_dictionary(input_dict):
   d = defaultdict(list)
   for v,k in input_dict.items():
       d[k].append(v)
       return d

But it returns this error though:

Error in evaluating function:
TypeError at line 6
unhashable type: 'list'
zondo

You can do it very simply like this:

newdict = {}
for key, value in olddict.items():
    for string in value:
        newdict.setdefault(string, []).append(key)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I select from a dictionary whose values are a list of lists in python2.7?

From Dev

How to Get a Key value from a dictionary whose values are lists?

From Dev

python getting dictionary values in lists

From Dev

python getting dictionary values in lists

From Dev

How to sort a dictionary by values which contain lists in python

From Dev

How to combine values composed of lists with common items in a dictionary using Python?

From Dev

How to reverse a list of lists in python?

From Dev

How to reverse a list of lists in python?

From Dev

I have nested lists as entries to a Python dictionary. How do I print the nested list whose first elements is "S"?

From Dev

I have nested lists as entries to a Python dictionary. How do I print the nested list whose first elements is "S"?

From Dev

Turning dictionary values into different lists - the dictionary values are tuples - Python

From Dev

How to convert lists in a dictionary to dictionaries in a lists (Python)

From Dev

Iterating through a python dictionary with lists as values

From Dev

Python: Create list of lists as dictionary values

From Dev

How to change values in a dictionary to lists instead of strings

From Dev

To reverse a dictionary and display values

From Dev

Reverse keys and values in dictionary

From Dev

Reverse keys and values in dictionary

From Dev

Accessing dictionary values that are lists of lists

From Dev

how to store lists as values in python defaultdict lists

From Dev

Knockout - dictionary with lists as values

From Dev

Python : reverse keys and values in dictionary considering strings and not caracters

From Dev

Python: How to get these lists transformed in a dictionary?

From Dev

How to simplify a dictionary of nested lists in python?

From Dev

Python 2.7.12: How to combine two lists into a dictionary

From Dev

How to group nested lists (python) into dictionary?

From Dev

Reverse Lists in Python

From Dev

How to reverse dictionary items and list keys grouped by common values

From Dev

How to get dictionary values in Python

Related Related

  1. 1

    How do I select from a dictionary whose values are a list of lists in python2.7?

  2. 2

    How to Get a Key value from a dictionary whose values are lists?

  3. 3

    python getting dictionary values in lists

  4. 4

    python getting dictionary values in lists

  5. 5

    How to sort a dictionary by values which contain lists in python

  6. 6

    How to combine values composed of lists with common items in a dictionary using Python?

  7. 7

    How to reverse a list of lists in python?

  8. 8

    How to reverse a list of lists in python?

  9. 9

    I have nested lists as entries to a Python dictionary. How do I print the nested list whose first elements is "S"?

  10. 10

    I have nested lists as entries to a Python dictionary. How do I print the nested list whose first elements is "S"?

  11. 11

    Turning dictionary values into different lists - the dictionary values are tuples - Python

  12. 12

    How to convert lists in a dictionary to dictionaries in a lists (Python)

  13. 13

    Iterating through a python dictionary with lists as values

  14. 14

    Python: Create list of lists as dictionary values

  15. 15

    How to change values in a dictionary to lists instead of strings

  16. 16

    To reverse a dictionary and display values

  17. 17

    Reverse keys and values in dictionary

  18. 18

    Reverse keys and values in dictionary

  19. 19

    Accessing dictionary values that are lists of lists

  20. 20

    how to store lists as values in python defaultdict lists

  21. 21

    Knockout - dictionary with lists as values

  22. 22

    Python : reverse keys and values in dictionary considering strings and not caracters

  23. 23

    Python: How to get these lists transformed in a dictionary?

  24. 24

    How to simplify a dictionary of nested lists in python?

  25. 25

    Python 2.7.12: How to combine two lists into a dictionary

  26. 26

    How to group nested lists (python) into dictionary?

  27. 27

    Reverse Lists in Python

  28. 28

    How to reverse dictionary items and list keys grouped by common values

  29. 29

    How to get dictionary values in Python

HotTag

Archive