Use the zip() function to subtract two lists in Python. You have to also use for-loop to iterate over the zip object and subtract the lists’ elements from each other and store the result in a new list.
zip(iterator1, iterator2)
Example Subtract two lists Python
Simple example code.
list1 = [1, 2, 3]
list2 = [1, 1, 1]
res = []
zip_obj = zip(list1, list2)
for i, j in zip_obj:
res.append(i - j)
print(res)
Output:
Another example with using list comprehension
Example list comprehensions used with the zip builtin function:
a = [1, 2, 3]
b = [1, 1, 1]
res = [a_i - b_i for a_i, b_i in zip(a, b)]
print(res)
Output: [0, 1, 2]
Do comment if you have any doubts or suggestions on this Python List tutorial.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.