Python - Assigning vertex weights is extremely slow
Usage of loops here is not clear.
You can:
import bpy

obj = bpy.context.active_object
vgroup = obj.vertex_groups.new(name=&quot;test_layer&quot;)

value = 0.5

for i in range(len(obj.data.vertices)):
    vgroup.add( [i], value, 'REPLACE')

We add vertices to a group, using their indexes.
The documentation is here:

add(index, weight, type)
Add vertices to the group

Parameters
        index (int array of 1 items in [-inf, inf]) – Index List
        weight (float in [0, 1]) – Vertex weight
        type (enum in ['REPLACE', 'ADD', 'SUBTRACT']) –
        Vertex assign mode
            REPLACE Replace, Replace.
            ADD Add, Add.
            SUBTRACT Subtract, Subtract.