How to handle absent textures procedurally?
The solution would be to automate creating the material nodes, that way you add the number of colours you want and then add the black filler at the end.

import bpy

obj = bpy.context.object
if obj.active_material is None:
    obj.active_material = bpy.data.materials.new('Material')

mat = obj.active_material
mat.use_nodes = True

mat_nodes = mat.node_tree.nodes
node_links = mat.node_tree.links
# remove everything except the output node
for n in mat_nodes:
    if n.type != 'OUTPUT_MATERIAL':
        mat_nodes.remove(n)

#  RGBA           red       green      blue
colour_list = [(1,0,0,1), (0,1,0,1), (0,0,1,1)]
data_imgs = bpy.data.images
# length should not be less than colour_list
img_list = [data_imgs['img.001'], data_imgs['img.002'],data_imgs['img.003']]

out_node = mat_nodes[0]
col_space = 200
padding = 40
prev_mix = None

for x in range(len(colour_list)):
    mix_node = mat_nodes.new('ShaderNodeMixShader')
    mix_node.location = (out_node.location.x - (mix_node.width+padding) - (col_space*x),
                        out_node.location.y)
    if x == 0:
        node_links.new(mix_node.outputs['Shader'], out_node.inputs['Surface'])
    else:
        node_links.new(mix_node.outputs['Shader'], prev_mix.inputs[1])
    prev_mix = mix_node

    img_node = mat_nodes.new('ShaderNodeTexImage')
    img_node.image = img_list[x]
    img_node.location = (mix_node.location.x - (img_node.width+padding),
                        mix_node.location.y + (img_node.height+padding+150))
    node_links.new(img_node.outputs['Color'], mix_node.inputs['Fac'])

    dif_node = mat_nodes.new('ShaderNodeBsdfDiffuse')
    dif_node.inputs['Color'].default_value = colour_list[x]
    dif_node.location = (mix_node.location.x - (dif_node.width+padding),
                        mix_node.location.y - (dif_node.height+padding))
    node_links.new(dif_node.outputs['BSDF'], mix_node.inputs[2])

fillin = mat_nodes.new('ShaderNodeBsdfDiffuse')
fillin.inputs['Color'].default_value = (0,0,0,1) # black
fillin.location = (prev_mix.location.x - (fillin.width+padding),
                    prev_mix.location.y)
node_links.new(fillin.outputs['BSDF'], prev_mix.inputs[1])