Assign Materials to specific faces in a mesh via Python
Based on your description of desired effects I've put together a couple small examples of how you may want to approach this.
You didn't really show what your materials should be or how you have them setup in your data structure so I provided a simple function to make basic materials around a hue wheel.
The other 2 functions will either evenly distribute materials to the faces of an object or as you described given a face start and run length of faces apply a specific material index to those faces.
You need to have all the materials you want to apply to an object's faces assigned to the object, then you can assign the material_index.
import bpy
import colorsys

num_colors = 3
repeat_color = 2
face_idx = 2
runlength = 4
mat_idx = 2
obj = bpy.context.object


def make_mats(obj, num_colors):
    hsv=[]
    for i in range(num_colors):
        hue = i/num_colors
        sat = 1
        val = 1
        hsv.append((hue, sat, val))
    # clear current materials
    while len(obj.material_slots) &gt; 0:
        bpy.ops.object.material_slot_remove()
    for col in hsv:
        if f'{obj.name}_{col[0]}' not in bpy.data.materials:
            mat = bpy.data.materials.new(f'{obj.name}_{str(col[0])}')
            r,g,b = colorsys.hsv_to_rgb(col[0], col[1], col[2])
            mat.diffuse_color = (r,g,b,1)
        else:
            mat = bpy.data.materials[f'{obj.name}_{str(col[0])}']
        bpy.context.object.data.materials.append(mat)


def assign_mats_even_dist(obj, repeat_color):
    for i, face in enumerate(obj.data.polygons):
        face.material_index = i // (repeat_color))%(len(obj.material_slots)


def assign_mats_idx_runlength(obj, face_idx, runlength, mat_idx):
    if mat_idx &gt; len(obj.material_slots)-1:
        return {'CANCELLED'}
    faces = [f for f in obj.data.polygons if f.index &gt;= face_idx and f.index &lt; face_idx + runlength]
    for face in faces:
        face.material_index = mat_idx


make_mats(obj, num_colors)

#assign_mats_even_dist(obj, repeat_color)

assign_mats_idx_runlength(obj, face_idx, runlength, mat_idx)