How to stack nodes using a loop?
import bpy

node_names = ['Texture Coordinate', 'Mapping', 'Image Texture','Image Texture 2','Diffuse BSDF','Material Output']
nodes = bpy.data.materials['Material'].node_tree.nodes
startlocy = 0
startlocx = 0


locx = startlocx
locy = startlocy
# Set the first node's X and y coords to 0,0
nodes[node_names[0]].location.x = locx
nodes[node_names[0]].location.y = locy
# For index, node in the list 'node_names'
for i, node in enumerate(node_names[1:]):
    # Set the last used node
    lastnode = node_names[i]
    #if the last node AND the current node are texture images
    if nodes[lastnode].type == "TEX_IMAGE" and nodes[node].type == "TEX_IMAGE":
        # Set current node to the height of the last one plus 200px
        locy += nodes[lastnode].height + 200
    else:
        # Otherwise, set the y to the default (0)
        locy = startlocy
        # Set the x to the width of the node plus 50px
        locx += nodes[lastnode].width + 50
    nodes[node].location.x = locx 
    nodes[node].location.y = locy 


This seems to work if your texture nodes have the same width, else only the highest node influences the next x position. You need to write the node names in the right order in the list node_names.