Looping through selected objects one at a time? OK, from the selected objects, I need to join one selected object with the active object and execute some other code. Then from the others select the next selected object and then execute the code again. I thought the following would work ,but it does all at the same.

import bpy

SEL_OBJS = bpy.context.selected_objects
ACT_OBJ = bpy.context.active_object    

#Join one at a time and execute more code
for OBJS in SEL_OBJS:
    bpy.ops.object.join()
    #More code here



Got my code working with the following.

import bpy

SEL_OBJS = bpy.context.selected_objects
ACT_OBJ = bpy.context.active_object

#List of all objects but the active object
SEL_NO_ACT = [o for o in SEL_OBJS if ACT_OBJ != o]

#Deselect list objects
for OBJS in SEL_NO_ACT: 
    OBJS.select_set(state=False)

#Loop list objects
while len(SEL_NO_ACT) &gt;= 1:
    OBJS = SEL_NO_ACT.pop()
    OBJS.select_set(state=True)
    bpy.ops.object.join()
    #Run code for each object after joining
Essentially you need to create a list of objects based on the current selection that will not change if the selection changes.

import bpy
# store selection in list that in future will not change with selection
sel_objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
bpy.ops.object.select_all(action='DESELECT')
if sel_objs:
    obj1 = sel_objs.pop()
    obj1.select = True
    bpy.context.scene.objects.active = obj1
while len(sel_objs) &gt;= 1:
    obj2 = sel_objs.pop()
    obj2.select = True
    bpy.ops.object.join()