Script that delete duplicated materials (from appending objects) and reassign the correct material I need to make a script that delete duplicated materials (from appending objects) and reassign the materials with the same names to the same material slots. Could you help me figuring out how to do it?

Something like that:

bpy.ops.wm.append (autoselect = True)
    #Check if 'bpy.context.active_object.active_material' has a dot on 4th position from the end (since duplicated material are renamed mat.001)
        #if condition true delete the mat
        #reassign mat with same name minus the 4 last characters of the name string on the same material slot
obj = bpy.context.active_object

for index in range(0, len(obj.material_slots.keys())):   

    obj.active_material_index = index

    if "." in obj.active_material.name:

        mat = obj.active_material.name[:-4]
        bpy.data.materials.remove(obj.active_material)
        obj.data.materials[index] = bpy.data.materials[mat] 


This work, I kind of cheated with the if "."