Some idea for list comprehansions?

Hello,

thats may code, it works perfect, but i have the feeling i have to much lines:


x = []
y = []
sum = []


for i in IN[0]:
	x.append(float(i))
	
for o in IN[1]:
	y.append(float(o))

for u,p in zip(x,y):
	z = u + p
	sum.append(z)
		
	
OUT = sum

I just convert string to float and i sum up it!

KR

Andreas

x = IN[0]
y = IN[1]
sum=[]

for i in range(len(x)):
    sum.append(float(x[i]) + float(y[i]))
    
OUT = sum
2 Likes

x = IN[0]
y = IN[1]
OUT = []

for i in range(len(x)):
    OUT.append(float(x[i]) + float(y[i]))

I am highly against using IN[0] & OUT in the middle of code, since it it not descriptive at all and it does not slow you code down if you assign variables to them, but if you want to do this in as few lines as possible I present you the 1 liner:

OUT = [float(IN[0][i]) + float(IN[1][i]) for i in range(len(IN[0]))]
7 Likes

@Draxl_Andreas Not an ideal solution but just wanted to give you another option!
OUT = [sum(x) for x in zip(map(float,IN[0]),map(float,IN[1]))]

2 Likes

Hello
another idea :grinning:

OUT = [sum(map(float, i)) for i in zip(*IN)]

3 Likes