Figuring out the correct context for an operation

Hey everyone,

I would like to run the shape_key_remove command on a list of objects using Python.
A snippet of the code I’m using is below.

    for object in bpy.data.objects:
        if (object.type == "MESH"):
            if (len(list(filter(lambda x : "mbastlab_proxyfit" in x.name, object.data.shape_keys.key_blocks))) > 0):
                print("Deleting proxy fitting keys from", object.name)
                def del_shape_key(name):
                    i = object.data.shape_keys.key_blocks.keys().index(name)
                    object.active_shape_key_index = i
                    bpy.ops.object.shape_key_remove()
                del_shape_key("Basis")
                del_shape_key("mbastlab_proxyfit")

The problem is, sometimes the code fails due to context issues!
Looking at the documentation for shape_key_remove() I don’t see any notes on what the necessary context is.
What’s the best way to approach the context issue?

Thanks!

Ensure your object is selected and active before operator call (like user will do by hand).
object.select = True
context.scene.objects.active = object

Thanks @stephen_leger, that’s working perfectly.