How to iterate over material index using Python
The fastest way to loop over the object's materials is through its material_slots attribute. Here's an equivalent code for toggling all object materials shadeless property:

import bpy

for m in bpy.context.object.material_slots:
    m.material.use_shadeless = not m.material.use_shadeless


The only issue I see in your own code is that you're assigning the material to a variable before changing active material index, which renders the latter operation moot. Changing the index before assigning the resulting material to a variable, will yield the result you want.