Python list is a data structure that allows you to create multiple objects in a single variable. If you have created already two lists and want to subtract them then this post is for you. In this entire post, you will learn how to subtract two lists in python using various methods.
Methods to Subtract two Lists in Python
Let’s learn all the methods to subtract two lists.
Method 1: For Loop
You can use for loop to subtract two lists. First, you have to create an empty list to store the result after subtraction of two items in the list in each iteration.
list1 = [10,30,40,50,60] list2 =[1,1,1,1,1] result_list =  for i in range(len(list1)): subtract = list1[i] - list2[i] result_list.append(subtract) print(result_list)
Method 2: Subtract two lists in python using the zip() function
Python has an inbuilt function that is zip() function that allows you to iterate the elements one by one from each input object. Here you will create an empty list and with each iteration subtract the elements one by one and append its result to the empty list.
list1 = [10,30,40,50,60] list2 =[1,1,1,1,1] result_list =  for i1,i2 in zip(list1,list2) : subtract = i1-i2 result_list.append(subtract) print(result_list)
Method 3: List Comprehension
List comprehension is a single-line statement that performs a function.Functionality is defined inside the square bracket(  ). You can subtract two lists using it by just defining the logic inside the square bracket. In my case, I have used the zip() function.
# List Comprehensions list1 = [10,30,40,50,60] list2 =[1,1,1,1,1] result = [l1-l2 for (l1,l2) in zip(list1,list2)] print(result)
Method 4: Use Numpy to subtract two lists
Numpy is a python module for numpy array creations. It allows you to perform mathematical calculations efficiently. In this method, you will first convert the existing list to the numpy array and then perform the simple subtraction. At last, you will convert the numpy array to a list using the list() function.
#numpy import numpy as np list1 = [10,30,40,50,60] list2 =[1,1,1,1,1] array1 = np.array(list1) array2 = np.array(list2) subtract = array1-array2 print(list(subtract))
Method 5: Subtract two lists using the pandas module
There is also another module that allows you to first create dataframe and then perform subtraction on the two lists. And it is pandas. Here you will convert the list to dataframe and then perform subtracting using the column name. The subtracted result will be converted to a list using the DataFrame.to_list() function.
#pandas import pandas as pd list1 = [10,30,40,50,60] list2 =[1,1,1,1,1] df = pd.DataFrame() df1 = pd.DataFrame(list1,columns=["list1"]) df2 = pd.DataFrame(list2,columns=["list2"]) df["subtract"] = df1["list1"]- df2["list2"] print(df["subtract"].to_list())
Method 6: Operator module
You can also use the operator module for performing subtraction among the lists. Here you have to first import the sub-module from the operator module and using the map function performs the subtraction on the two lists.
# operator from operator import sub list1 = [10,30,40,50,60] list2 =[1,1,1,1,1] result =list( map(sub, list1, list2)) print(result )
Method 7: Using the lambda function
The last method to do the subtraction of the two lists is the lambda function. Here the lambda function will contain the logic for subtraction and it will be mapped with the lists to find the result.
#lambda list1 = [10,30,40,50,60] list2 =[1,1,1,1,1] subtract = list(map(lambda x, y: x - y, list1, list2)) print(subtract)
In the previous post, you learned how to add two lists in python. The same methods can be used to subtract two lists in python. These are the methods to subtract two lists easily.
If you want to add your methods to this post then contact us. We will definitely look into your suggestion.