{"id":26653,"date":"2022-04-02T20:18:28","date_gmt":"2022-04-02T14:48:28","guid":{"rendered":"https:\/\/python-programs.com\/?p=26653"},"modified":"2022-04-02T20:18:28","modified_gmt":"2022-04-02T14:48:28","slug":"how-to-remove-duplicates-from-a-dictionary-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-remove-duplicates-from-a-dictionary-in-python\/","title":{"rendered":"How to Remove Duplicates from a Dictionary in Python?"},"content":{"rendered":"
The term duplicates<\/strong> mean the elements which are repeated.<\/p>\n When working with Python dictionaries, you may encounter circumstances where there are duplicate values in the dictionary. In some cases, this can be harmful to your software. In this instance, you may need to remove the duplicates from your dictionary.<\/p>\n When working with Python dictionaries, we may encounter a problem in which we need to delete all duplicate values from the dictionary, and we are unconcerned whether some keys are removed in the process. This type of application can be seen in both school programming and day-to-day programming.<\/p>\n The below-given ways can be used to remove duplicates from a dictionary in\u00a0 python:<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Here, we are printing the value: key<\/strong>\u00a0format<\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the method to remove the duplicates from the given dictionary in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n This is simply a shortcut for the operations described above. Here we print the value first followed by a key.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" The term duplicates mean the elements which are repeated. When working with Python dictionaries, you may encounter circumstances where there are duplicate values in the dictionary. In some cases, this can be harmful to your software. In this instance, you may need to remove the duplicates from your dictionary. When working with Python dictionaries, we …<\/p>\n\n
Given dictionary = {1:\u00a0100,\u00a02:\u00a090,\u00a03:\u00a080,\u00a04:\u00a0100,\u00a05:80}<\/span><\/pre>\n
The given dictionary: {1: 100, 2: 90, 3: 80, 4: 100, 5: 80}\r\nThe result dictionary after the removal of duplicates: {1: 100, 2: 90, 3: 80}<\/pre>\n
Given dictionary = {10:\"hello\", 20:\"Python-programs\", 30:\"good\", 40:\"morning\", 50:\"hello\"}<\/span><\/pre>\n
The given dictionary:\r\n{10: 'hello', 20: 'Python-programs', 30: 'good', 40: 'morning', 50: 'hello'}\r\nThe result dictionary after the removal of duplicates:\r\n{'hello': 50, 'Python-programs': 20, 'good': 30, 'morning': 40}<\/pre>\n
Removing Duplicates from a Dictionary in Python<\/h2>\n
\n
Method #1: Using For loop<\/h3>\n
\n
# Give the dictionary as static input and store it in a variable.\r\ngvn_dict ={1: 100, 2: 90, 3: 80, 4: 100, 5:80}\r\n# Print the given dictionary\r\nprint(\"The given dictionary:\",gvn_dict)\r\n# Take an empty list and store it in aother variable.\r\nnew_lst =[]\r\n# Create a new empty dictionary and store it in aother variable.\r\noutput_dict ={}\r\n# Loop in the key, value of the given dictionary using the for loop and the items() functions\r\nfor key, value in gvn_dict.items():\r\n # Check if the corresponding value is not present in the above created new list using the \r\n # if conditional statement\r\n if value not in new_lst:\r\n # If it is true then append that value to the above list \r\n # Here it appends the value to the new list if it is a unique element\r\n new_lst.append(value)\r\n # Assign this unique key and value to the new dictionary\r\n output_dict[key] = value\r\n# Print the result dictionary after the removal of duplicates\r\nprint(\"The result dictionary after the removal of duplicates:\",output_dict)<\/pre>\n
The given dictionary: {1: 100, 2: 90, 3: 80, 4: 100, 5: 80}\r\nThe result dictionary after the removal of duplicates: {1: 100, 2: 90, 3: 80}<\/pre>\n
Method #2: Using Dictionary Comprehension method<\/h3>\n
\n
# Give the dictionary as static input and store it in a variable.\r\ngvn_dict ={10:\"hello\", 20:\"Python-programs\", 30:\"good\", 40:\"morning\", 50:\"hello\"}\r\n# Print the given dictionary\r\nprint(\"The given dictionary:\\n\",gvn_dict)\r\n# Create another dictionary with key as values of original dictionary and\r\n# values as keys of the original dictionary using dictionary comprehension\r\noutput_dict = {value:key for key, value in gvn_dict.items()}\r\n# Print the result dictionary after the removal of duplicates\r\nprint(\"The result dictionary after the removal of duplicates:\\n\",output_dict)<\/pre>\n
The given dictionary:\r\n{10: 'hello', 20: 'Python-programs', 30: 'good', 40: 'morning', 50: 'hello'}\r\nThe result dictionary after the removal of duplicates:\r\n{'hello': 50, 'Python-programs': 20, 'good': 30, 'morning': 40}<\/pre>\n