Ancient Oak
Ancient Oak

Reputation: 15

Multiple List Individual append

Anyone know if there is a way to do something like this?:

def function(a, b, z):
    code
    return x, y, z

X, Y, Z = [] for i in range(3)
for a in range(rows):
    for b in range(cols):
         X.append(), Y.append(), Z.append() = function(a, b, z)

so that on each iteration each corresponding value goes to its proper list

Upvotes: 0

Views: 33

Answers (1)

chepner
chepner

Reputation: 532238

No; you have to unpack the return value first, then pass each value to the appropriate function.

v1, v2, v3 = function(a, b, z)
X.append(v1)
Y.append(v2)
Z.append(v3)

By the way, X, Y, Z = [] for i in range(3) isn't valid syntax. Just write

X = []
Y = []
Z = []

You might consider a list (or dict) of lists instead of separate names X, Y, and Z.

results = [[], [], []]
for a in range(rows):
    for b in range(cols):
        vs = function(a, b, z)
        for lst, v in zip(results, vs):
            lst.append(v)

but for only 3 lists, I don't think this gains you much.

Upvotes: 1

Related Questions