In this article, we will be solving a problem statement of python subtract lists element by element. This problem we will be solving in 5 different ways, and discuss each method used to solve this problem in detail.
What is a List in Python? The list is one of the 4 built-in data types in Python used to store data that is mutable, can be ordered, and can be duplicated within the list. The list is created by placing all the elements within the square brackets and separated by commas.
list1 = [1,2,3,'john',7.5,3]
Table of Contents
How to subtract lists element by element in python
There are many ways to subtract lists elements wise in python, below we will be discussing 5 ways to do so.
Method 1- Python Subtract lists using the zip() method
The python zip method takes zero or more iterables and returns an iterable object. This iterable object contains the series of tuples containing the element of each iterable given as input.
Iterables – any python iterable like string, list, tuple, or dictionary.
Zip() method return value-
- If we do not pass any parameter it will return an empty iterator
- If multiple iterators are passed, it returns an iterator of tuples, where each tuples tuple contains elements from all iterators passed as input.
list_a = [10, 9, 8, 7] list_b = [2, 3, 4, 5] # initalize empty list to stored difference between lists subtracted_list =  # iterating on iterator object return by zip() method for i,j in zip(list_a, list_b): subtracted_list.append(i - j) print("Subtracted List ->",subtracted_list)
Subtracted List -> [8, 6, 4, 2]
Method 2- Python Subtract lists element by element using the Naive method
In this method, we will be using python for loop having the range from 0 to the length of the list. And in each iteration, we will subtract lists elements.
# defining lists list_a = [10, 9, 8, 7] list_b = [2, 3, 4, 5] # initialize the subtracted list subtracted_list =  # Using for loop to subtract lists for i in range(len(list_a)): subtracted_list.append(list_a[i] - list_b[i]) print("Subtracted List is ->", subtracted_list)
Subtracted List is -> [8, 6, 4, 2]
Method 3- Python Subtract lists using the NumPy subtract() method
Numpy in python is used for scientific computing. It consists of multidimensional array objects and methods and functions to process those array objects. To learn about NumPy in python you can read here.
In python to subtract two subtract lists, we can use NumPy subtract() method. The Numpy subtract() method returns the element-wise difference between the two arrays. The Numpy subtract() takes 2 Numpy arrays as input.
Steps to follow to get subtracted list-
- Define two lists for difference
- Convert the python lists into a Numpy array by using the Numpy array() method
- Subtract those arrays using Numpy subtract method.
- Convert the Numpy array into the python list.
- Display the output.
# importing numpy package import numpy as np # defining list list_a = [10, 9, 8, 7] list_b = [2, 3, 4, 5] # comverting list to numpy array array_1 = np.array(list_a) array_2 = np.array(list_b) # Using numpy subtract function to get element wise subtracted array subtracted_array = np.subtract(array_1,array_2) print("Subtracted Numpy array is", subtracted_array) # Converting Numpy array to list subtracted_list = list(subtracted_array) print("Subtracted List is", subtracted_list)
Subtracted Numpy array is [8 6 4 2] Subtracted List is [8, 6, 4, 2]
Also Read: 4 ways to count occurrences in list Python
Method 4- Using List Comprehension
To subtract lists in python we also use list comprehension. In Python, list comprehension is a way to create a new list from existing other iterables like lists, sets, and tuples. We can also say that list comprehension is a shorter version of for loop. To know about list comprehension you can visit this.
# defining list list_a = [10, 9, 8, 7] list_b = [2, 3, 4, 5] # Using list comprehension subtracted_list = [x - y for x,y in zip(list_a, list_b)] print("Subtracted List is", subtracted_list)
Subtracted List is [8, 6, 4, 2]
Method 5- Python Subtract lists using the set
In this method, we convert both lists into sets, and use set differences, to find the element-wise difference between the list element. Since we are using the sets, we will get the differences of only all unique elements.
# defining list list_a = [10, 9, 8, 7] list_b = [2, 3, 4, 5] # converting list to set set_a = set(list_a) set_b = set(list_b) subtracted_list = list(set_a - set_b) print("Subtracted List is", subtracted_list)
Subtracted List is [8, 9, 10, 7]
Hence we have seen python subtract lists element by element. We have done using zip(), naive method, using list comprehension, and NumPy subtract method.