A
A
Artyom Innokentiev2015-12-14 14:08:38
Python
Artyom Innokentiev, 2015-12-14 14:08:38

How to "beautifully" and shortly subtract two arrays element by element?

There are arrays:

a = [1,2,3,4]
b = [5,6,7,8]

It is necessary to receive:
c= [5-1, 6-2, 7-3, 8-4] => c=[4, 4, 4, 4]
To write function for this business - I think, it is too bold and long. I am sure that there are native tools or something like a list comprehension that solve the task in a short and beautiful way.
What are the means?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2015-12-14
@artinnok

[x - y for x, y in zip(b, a)]

V
Vladimir Martyanov, 2015-12-14
@vilgeforce

That function will probably have 5 lines: it will take longer to look for something ready than to write it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question