Python - Merging vertices on distance and delete vertices (connected to only 2 vertex)

Hi all, i would like some help in this counting mess , XD.

I made a tool to draw " free edges and vertices "
I have a mesh with N vertex( old_v ), and i draw/add N more vertex with edges.
with a proper index, and i store these index in ( new_v )

  1. i cant make a script loop , that is able to merge multiple green spots,
  2. i cant make Detect all vertex that are connected to two vert only,
    And than delete them all. so the remained vertex can have 2connected but dont be deleted.

Right now i am using the deformer “Weld” but he merge also vertex of the same old_v togheter and it ruin the drawing shape.

Whoa, I finally get the counting right ,
Here is the code to search for all the not-Useful vertex and delete them

bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active=bpy.data.objects[NAME] 
 
VVV=[]
VVV.clear()
print("----")
for v in bpy.data.objects[ NAME].data.vertices:
    VVV.append(0)
#    print("VERT")
for e in bpy.data.objects[NAME].data.edges:
    VVV[e.vertices[0]]+=1
    VVV[e.vertices[1]]+=1
i=0
for L in VVV:
    if L <= 2 :
        bpy.data.objects[NAME].data.vertices[i].select=True
        print(bpy.data.objects[NAME].data.vertices[i].co) 
    i+=1
    print(i)          
#print ( "LENGT " + str( len( VVV) ))

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.dissolve_verts()
bpy.ops.mesh.delete(type='VERT')
bpy.ops.object.mode_set(mode='OBJECT') 

Now Fixed the modifier too.
to apply it you need to use the “id” name of the modifier,
So we can get it from the array of modifier of the object

bpy.ops.object.select_all(action='DESELECT')
    bpy.context.view_layer.objects.active = None                  # DESELECT ALL

    obj = bpy.data.objects[ bpy.context.scene.QS_DATA.MeshName ]  # OBJ by name     
      
    bpy.context.view_layer.objects.active = obj
    obj.select_set(True)                                          # SELECT OBJ

    bpy.ops.object.modifier_add(type='WELD' )                     # ADD WELD Modifier
     
    for mod  in obj.modifiers:                                    # APPLY MOD
        if mod.type=="WELD":
            mod.merge_threshold =bpy.context.scene.QS_DATA.Qmerge 
            bpy.ops.object.modifier_apply(modifier= mod.name)       

    bpy.ops.object.select_all(action='DESELECT')
    bpy.context.view_layer.objects.active = None