Python conditionals lists

Hello people,

I need some assistance in python scriping.

I have two lists with the same length and I need to create a third list which will be like this:

if the first element in the list X is 0, the first element on the outcome should be ‘the first element of the list Y’
if the first element in the list X is 1, the first element on the outcome list should be 0.

and so on for each element.

therefor the length of the list will be again 15.

can someone help me how to implement this in python script?

many thanks in advance,python_conditions.gh (3.2 KB)

Hello,

How’s this?


def main():
	list_x = [1,0,0,1]
	list_y = [2,4,6,8]
	list_z = []
	for x, y in zip(list_x,list_y):
		if x==0:
			list_z.append(y)
		elif x==1:
			list_z.append(0)
	print(list_z)
		
if __name__=='__main__':
	main()

[0, 4, 6, 0]

Or in one line for fun :wink:

list_z=[(y,0)[x] for x,y in zip(list_x,list_y)]
1 Like

Thanks a lot,
I tried but somehow idk how it works exactly, could you please check it in gh.python_C.gh (5.8 KB)

rslt = []
for i,j in zip(x,y):
    rslt.append(j if (i==0) else 0)

a = rslt

python_conditions_2020Jan18a.gh (5.9 KB)

3 Likes

thanks a lot :slight_smile: