1

For example if I have the lists:

x = [1,2,3,4,5,6]
y = [4,3,2,5,6,7]

How do I subtract my lists in order to get:

z = "x-y" = [1-4,2-3,3-2,4-5,5-6,6-7]
z = [-3,-1,1,-1,-1,-1]

?

Meaning, as long as the numbers from my lists share the same index then subtract them.

Thanks in advance.

1 Answers1

0

You can utilize the Python zip method which will combine the two lists into one list of tuples and iterate that list:

x = [1,2,3,4,5,6]
y = [4,3,2,5,6,7]

z = [a - b for a, b in zip(x, y)]
Omari Celestine
  • 1,405
  • 1
  • 11
  • 21