Subtract Two Lists Python

Subtract Two Lists Python | Here we will develop a program to subtract two lists in python. We will give two lists and the python program will subtract these lists using set() and without using set(). We will also develop a python program to subtract lists element by element using built-in function zip() methods and numpy.subtract() methods.

How to subtract two lists in Python:

First take and store two lists, assume we stored them in “a”, and “b” variable, then to substract them use expression: (a – b). Example:-

a = [0, 1, 2, 3, 4, 5,6]
b = [0, 2, 5]
a-b = [1, 3, 4,6]

How to subtract lists element by element:

a = [10, 15, 20, 30, 40]
b = [5, 8, 20, 40, 25]
a-b = [5, 7, 0, -10, 15]

Python Subtraction Between Two Lists

We will take two lists while declaring the variables. Then, convert the list to set using set() function and subtract sets. Finally, the subtraction value will be displayed on the screen. The set() function creates a set object. The items in a setlist are unordered, so they will appear in random order.

# Python program to subtract two lists

# take list
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [1, 3, 4, 7, 9]

# print original list
print('list1 =', a)
print('list2 =', b)

# subtraction of list
sub = list(set(a) - set(b))

# print subtraction value
print('list1 - list2 =', sub)

Output:-

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [1, 3, 4, 7, 9]
list1 – list2 = [8, 2, 5, 6]

Subtract Two arrays

In the previous program, we used the set() function but in this program, we will subtract 2 lists without using the set() function.

# Python program to subtract two lists

# take list
a = [10, 20, 30, 40, 50, 60, 70, 80, 90]
b = [20, 30, 60, 80]

# print original list
print('list1 =', a)
print('list2 =', b)

# subtraction of list
sub = [i for i in a if not i in b or b.remove(i)]

# print subtraction value
print('list1 - list2 =', sub)

Output:-

list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90]
list2 = [20, 30, 60, 80]
list1 – list2 = [10, 40, 50, 70, 90]

Python Subtract Lists Element by Element

In this program, we will give two lists. Then, subtract all elements present in the list and store them in a sub variable using the For Loop. Finally, the subtraction value will be displayed on the screen.

# Python program to subtract lists element by element

# take list
a = [20, 25, 30, 40, 55, 15]
b = [5, 12, 35, 40, 45, 28]

# print original list
print('list1 =', a)
print('list2 =', b)

# subtraction of element
sub = []
for i in range(len(a)):
    sub.append(a[i] - b[i])

# print subtraction value
print('list1 - list2 =', sub)

Output:-

list1 = [20, 25, 30, 40, 55, 15]
list2 = [5, 12, 35, 40, 45, 28]
list1 – list2 = [15, 13, -5, 0, 10, -13]

Subtract All Elements in Array

This python program also performs the same task but with different methods. In this program, we are using a built-in function. The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together, etc.

# Python program to subtract lists element by element

# take list
a = [20, 25, 30, 40, 55, 15]
b = [10, 35, 30, 26, 67, 12]

# print original list
print('list1 =', a)
print('list2 =', b)

# subtraction of element
sub = [x-y for (x, y) in zip(a, b)]

# print subtraction value
print('list1 - list2 =', sub)

Output:-

list1 = [20, 25, 30, 40, 55, 15]
list2 = [10, 35, 30, 26, 67, 12]
list1 – list2 = [10, -10, 0, 14, -12, 3]

Subtract Function in Python

The numpy.subtract() function is used when we want to compute the difference of two numbers or arrays. It returns the difference of numbers.

# Python program to subtract lists element by element

# importng numpy.subtract()
import numpy

# take list
a = [10, 14, 8, 64, 54, 47]
b = [10, 33, 45, 12, 54, 23]

# print original list
print('list1 =', a)
print('list2 =', b)

# subtraction of element
sub = numpy.subtract(a, b)

# print subtraction value
print('list1 - list2 =', sub)

Output:-

list1 = [10, 14, 8, 64, 54, 47]
list2 = [10, 33, 45, 12, 54, 23]
list1 – list2 = [0 -19 -37 52 0 24]

Also See:- Compound Interest Program in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *