Extrude changes faces index

I’m new to blender scripting. I’m trying to make a simple house generator and I noticed that whenever I extrude a face the side faces generated by the extrusion change index every time I run the script.
So if I want to select in the script one of those faces by index and do something with that it’ll break the next time I run the script.
There’s a way to prevent this to happend?

since you’re new to blender scripting I’m assuming you’re extruding by using bpy.ops.mesh.extrude, which is the operator that a user fires off when they want to extrude something. It’s not really suitable for what you’re trying to do because, as you noticed, the indices change after each mesh operation and you can’t rely on them to remain static (and rightly so). What you need to be using is bmesh, and the bmesh operators- which will return the new faces that were created during the extrusion process. you can also tag faces before any operator to ‘remember’ a face, then recall it later if necessary.

TLDR: for complex mesh operations use bmesh.