indexerror: bpy_prop_collection[index]: index 0 out of range, size 0 I'm trying to run the below script. But for some reason I receive this error saying

Traceback (most recent call last):
File "&lt;blender_console&gt;", line 1, in &lt;module&gt;
File "Text", line 18, in &lt;module&gt;
File "Text", line 18, in &lt;listcomp&gt;
indexerror: bpy_prop_collection[index]: index 0 out of range, size 0


Below is the script I'm using and in the output it prints 1 only, so this means the error is in this statement labels = [  v.groups[0].group for v in mesh.vertices ]

So if anyone could please advise

import bpy

scn = bpy.context.scene
mesh_obj = scn.objects.active
mesh = mesh_obj.data

vertex_groups = mesh_obj.vertex_groups
print("1")


labels = [  v.groups[0].group for v in mesh.vertices ]
print("2")
Your assumption is that there is at least one group (groups[0]) that can be accessed, but it's not the case if the vertex isn't assigned to any group. It's independent of the existing vertex groups of the object.

If you want a list of all vertex group names, use this instead:

labels = [vgroup.name for vgroup in mesh_obj.vertex_groups]


For a list of all vertex groups the current vertex is part of, use:

# Create a lookup dictionary once
vgroup_mapping = {i: vgroup.name for i, vgroup in enumerate(mesh_obj.vertex_groups)}

# Look up the vgroup names for a certain vertex (could also iterate over all vertices here)
vert = mesh.vertices[0]
labels = [vgroup_mapping[group.group] for group in vert.groups]
print(labels)