 # 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:
x.append(float(i))

for o in IN:
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
y = IN
sum=[]

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

OUT = sum``````
2 Likes
``````
x = IN
y = IN
OUT = []

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

I am highly against using IN & 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[i]) + float(IN[i]) for i in range(len(IN))]``
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),map(float,IN))]`

2 Likes

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

3 Likes