-1

Here is my code to create a dictionary with no duplicates. I am getting error AttributeError: 'dict' object has no attribute 'append'. Thank you for your help.

       mydict = {"c" : "2", 
                "b" : "3", 
                "a" : "4", 
                "d" : "1",
                "e" : "1",
                "f"  :"1"
              
                }

dup_free= {}
for key, val in mydict.items():
    if (key, val not in dup_free.items()):
        dup_free.append(key, val)
SBh
  • 1
  • 1
  • There is no append method to add an item to a dictionary in Python. You can try `dup_free[key]=val` instead of `dup_free.append(key, val)`. – fenderogi Nov 01 '22 at 13:46

1 Answers1

1

As the error says, dict has no append method. You are probably thinking of a list. Also, because python dicts cannot have duplicate keys, your dup_free dict will always be the same as your mydict.

jprebys
  • 2,469
  • 1
  • 11
  • 16