How delete a bpy_prop_collection element


In an object, I have added a PropertyGroup with something like this:

mp = mainobject.MainData[0]


How can I delete an element of maindata_segments by index?

something like


I have tried several ideas but nothing works, any idea?

Have you tried del maindata_segments[5]?

If that doesn’t work, this particular list property doesn’t support item assignment/deletion. You should look into the related documentation if there are functions to achieve what you want!

The result is:

TypeError: del bpy_prop_collection[key]: not supported

The object you call .add() on has also a .remove() method, which expects the index number of the element to be removed.

You could do the following:

mp = mainobject.MainData.add() # mainobject.MainData[0] = "foo"

idx = mainobject.MainData.find("foo")
if idx > -1:

But in certain cases, the following would be more reliable:

for i, elem in enumerate(mainobject.MainData):
    if elem == mp:

Thanks CoDEmanX, the remove() works. I had tested all combinations with delete() and del, but I didn’t think about remove()

Where did you find the documentation for this? I was looking the API docs and I was not able to find it. I think I’m not use to this api docs :slight_smile:

Auto-complete in Blender’s Python console reveals this function, the API docs don’t list the methods of property collections IIRC (not sure why).