components = { "U1": ("0Ohm", "10V", "IU1"), "R1": ("1e3Ohm", "UR1", "IR1"), "R2": ("5e3Ohm", "UR2", "IR2"), } nodes = {"n1", "n2"} connections = { "U1": ["n1"], "n1": ["R1", "R2"], "R1": ["n2"], "R2": ["n2"], "n2": ["U1"], } nodeCoeffs = {} for n in nodes: nodeCoeffs[n] = [ f"-{components[k][2]}" if n in v else f"{components[x][2]}" for k, v in connections.items() for x in v if k == n or n in v ] print(nodeCoeffs)