'Creating two 1D lists from python based on function returning two entries

I have a function f(x,y) which returns two values a,b.

I want to construct a 2D List from returned values of a,b from f being called on x in a List of values and y in an equally long list of values.

Is there an easy way to do this?

I tried this and it did not work.

aList, bList = [f(x[i],y[i],1) for i in range(T)]


Solution 1:[1]

You always need to think about what you HAVE vs what you WANT. The list comprehension returns a single list containing 2-tuples. You need to split that list.

result = [f(x[i],y[i],1) for i in range(T)]
aList = [k[0] for k in result]
bList = [k[1] for k in result]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts