Skip to main content

How to remove duplicate keys from dictionary in python

This python code will remove all duplicate keys and associate items from the dictionary. To remove we are using one temp array and using it for loop iterating each value.

thisdict = {
  "1": "C language",
  "2": "Java Language",
  "4": "Python Language",
  "3": "C++",
  "4": "Python Language",
  "3": "C++",
  "5": "C++",
#declare temp array
tempA = []  
uniqueDict = dict()
for key, val in thisdict.items():
    if val not in tempA:
        uniqueDict[key] = val

{'1': 'C language', '2': 'Java Language', '4': 'Python Language', '3': 'C++'}

Leave a Reply

Your email address will not be published. Required fields are marked *