Apply modifiers

You misunderstand. That list is not ordered in any particular way. Blender is not keeping track of selection order, other than to know which object is active. See e.g.

Anyway, without a more clear understanding what you’re trying to do it’d be hard to help you.

If, for example, you want to boolean union together all selected objects, then, in order to get reliable results, you’d need to pick one of them (i.e. first), and union all the others with it. If you just walk the list like you do, even assuming you’d fix the crash, you’ll be getting unpredictable results.