For The RNA mesh API, your example is quite good, one minor change you can make is to break
once the group index is found.
Instead of making a large list, you could use a generator function, this can save memory for the cases you can iterate over the elements, and be converted to a list or tuple if you need, it has the same worst-case time, but in practice will often save some looping.
import bpy
obj = bpy.context.object
def get_weights(ob, vgroup):
group_index = vgroup.index
for i, v in enumerate(ob.data.vertices):
for g in v.groups:
if g.group == group_index:
yield (i, g.weight)
break
import bpy
obj = bpy.context.object
vgroup = obj.vertex_groups[0]
# iterate over index, weights
for i, w in get_weights(obj, vgroup):
print(i, w)
# if you need as a list...
weights = list(get_weights(obj, vgroup))
print(weights)
Note, in some similar cases to this, you can use list-comprehension, however having to check the vertex is in the group causes the checks within the list-comprehension to become quite convoluted, so best avoid in this case.