In this tutorial, you’ll learn How to Subtract two lists in Python. Before performing list subtraction, keep in mind that both lists should be of the same length and all the elements should be of the same datatype.
For instance, suppose you have two lists and you want to perform a subtraction between these two lists i.e.,
Input list 1 = [7,6,2,4,-2,8,9]
Input list 2 = [2,9,-3,0,9,5,6]
Output:
Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6]
= [5,-3,5,4,-11,3,3]
Methods to Subtract two Lists in Python
When it comes to subtracting two lists in Python, there are several commonly used methods. One approach is to utilize the zip()
function, which pairs corresponding elements from both lists and allows for their subtraction. Another option is to employ list comprehension, which enables concise creation of a new list by subtracting corresponding elements. Additionally, the powerful Numpy library provides the capability to subtract lists by treating them as Numpy arrays. Lastly, a basic for loop can be utilized to subtract elements between two lists iteratively.
1) Using Zip() Function
In this method, we’ll pass the two input lists to the Zip Function. Then, iterate over the zip object using for loop. On every iteration, the program will take an element from list1 and list2, subtract them and append the result into another list.
# Create and initialize two lists
list1 = [9,1,3,7]
list2 = [4,4,5,6]
#initialize a variable which will store the difference of two lists
result = []
for i, j in zip(list1,list2):
result.append(i - j)
print(result)
# OUTPUT:
# [5, -3, -2, 1]
- An empty list named result is initialized. This list will store the differences between corresponding elements of list1 and list2.
- The zip() the function is used to iterate over the elements of both lists simultaneously. It pairs up the elements at the same index from list1 and list2.
- In each iteration of the loop, the variables i and j receive the corresponding elements from list1 and list2.
- The difference between i and j is calculated using the subtraction operator (-), and the result is appended to the result list using the append() method.
- After all the iterations are completed, the result the list will contain the differences between the corresponding elements of list1 and list2.
- Finally, the result list is printed to the console using the print() function
2) Using List Comprehension
Another way to subtract two lists is by using list comprehension. For this, you need to traverse over the lists and perform subtraction one by one of all elements as shown in the code snippet below.
#create and initialize two lists
list1 = [9,1,3]
list2 = [4,4,5]
#perform subtraction and store the result in "difference"
difference = [List1[i]-List2[i] for i in range(min(len(list1), len(List2)))]
#print the difference of two lists
print(difference)
# Output:
# [5, -3, -2]
The provided Python code creates and initializes two lists, list1
and list2
. It then calculates the difference between corresponding elements of the two lists and stores the results in a new list called difference
. Finally, it prints the difference
list.
3) Using Numpy Array
The previous two methods require traversal over the whole list. One of the simplest methods is to convert the two lists into an array. Here, np.array() function converts two lists into arrays and then uses subtract operator.
#create and initialize two lists
list1 = [2,3,9,-4,7]
list2 = [4,-1,5,3,8]
#convert the two lists into arrays and store the difference
difference = np.array(list1)-np.array(list2)
#print the difference of two lists
print(difference)
# Output:
# [-2 4 4 -7 -1]
4) Using For Loop
To subtract two lists in Python using a for loop, you can iterate over the elements of one list and check if each element exists in the other list. If it doesn’t, you can add it to a new list. Here’s an example:
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
subtracted_list = []
for item in list1:
if item not in list2:
subtracted_list.append(item)
print(subtracted_list)
# [1, 2]
In the above code, we have two lists list1 and list2. We initialize an empty list subtracted_list to store the result of the subtraction. Next, we iterate over each element in list1 using a for loop. Inside the loop, we check if the current element (item) is present in list2 using the not-in operator. If it’s not found, we append it to the subtracted_list using the append() method. Finally, we print the subtracted_list, which contains the elements that are present in list1 but not in list2. Note that this method assumes that the order of elements in the lists doesn’t matter. If the order matters, you might need to use a different approach, such as list comprehension or set operations.
If you have any queries regarding this article, contact us. Your feedback matters a lot. See more Python Tutorials