How to subtract lists in Python? List subtraction involves subtracting the corresponding elements from one list to another, producing a new list with the differences between each pair of elements at the same index. This operation is indeed similar to performing subtraction in mathematics. To perform list subtraction, the two input lists must have the same length. The subtraction operation is applied to each pair of elements at the same index in both lists. The resulting differences are then stored in a new list, preserving the order of the elements.
You can use list subtraction in Python using many ways, for example, by using the zip(), list comprehension, lambda, map(), – operator, operator.sub(), enumerate(), for loop, and np.subtract() functions. In this article, I will explain list subtraction by using all these methods with examples.
1. Quick Examples of Subtracting the List
If you are in a hurry, below are some quick examples of list subtraction.
# Quick examples of list subtraction
# Initialization lists
list1 = [2, 8, 5, 9, 3]
list2 = [1, 4, 6, 2, 8]
# Example 1: List Subtraction
# Using zip() function
result = [x – y for x, y in zip(list1, list2)]
# Example 2: Using the list comprehension and zip()
result = [round(x-y, 2) for x, y in zip(list1, list2)]
# Example 3: Using the lambda function with map() function
subtracted_list = list(map(lambda x, y: x-y, list1, list2))
# Example 4: List subtraction
# Using zip() and “-” operator
sub_list = []
zip_object = zip(list1, list2)
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append(list1_i – list2_i)
# Example 5: Using operator.sub() method
sub_list = list(map(operator.sub, list1, list2))
# Example 6: Using enumerate() function
sub_list = [element – list2[i] for i, element in enumerate(list1)]
# Example 7: Using for loop
sub_list = []
for i in range(len(list1)):
element = list1[i] – list2[i]
sub_list.append(element)
# Example 8: Using np.subtract() method
array1 = np.array(list1)
array2 = np.array(list2)
sub_array = np.subtract(array1, array2)
sub_list = list(sub_array)
2. List Subtraction Using zip() function
You can perform list subtraction using the zip() function. Simply, you can iterate over the zipped pairs of elements from the two lists and subtract them. For example, the zip() function is used to pair up the corresponding elements from list1 and list2. Then, a list comprehension subtracts the elements at each index using the variables x and y. The resulting list result contains the differences [1, 4, -1, 7, -5], which are the results of subtracting list2 from list1 element-wise.
# Initialize the lists
list1 = [2, 8, 5, 9, 3]
list2 = [1, 4, 6, 2, 8]
# List Subtraction
# Using zip() function
result = [x – y for x, y in zip(list1, list2)]
print(“First list:”, list1)
print(“Second list:”, list2)
print(“After subtracting the list:”, result)
Yields below output.
3. Using the List Comprehension and zip() Function
You can also calculate list subtraction using list comprehension and the zip() function. It subtracts corresponding elements from list2 from list1 and creates a new list with the results.
In the below example, zip(list1, list2) pairs up the elements at corresponding indices from list1 and list2. The list comprehension subtracts each pair of elements (x – y), rounds the result to 2 decimal places using round(), and stores the results in the result list. Finally, it prints the subtraction list, which is [1,4,-1,7,-5].
# Using the list comprehension and zip()
result = [round(x-y, 2) for x, y in zip(list1, list2)]
print(“After subtracting the list:”, result)
Yields the same output as above.
4. Using the Lambda Function with map() Function
You can also perform list subtraction using a lambda function and the map() function. It subtracts corresponding elements from list2 from list1 and creates a new list with the results.
The below example lambda x, y: x – y defines a lambda function that subtracts the corresponding elements from list2 from list1. The map() function applies this lambda function to each pair of elements from list1 and list2. The resulting subtracted values are collected subtracted_list using the list() function.
# Using the lambda function with map() function
subtracted_list = list(map(lambda x, y: x-y, list1, list2))
print(“After subtracting the list:”, subtracted_list)
Yields the same output as above.
5. List Subtraction Using zip() and “-” Operator
You can also perform list subtraction using the zip() function and the subtraction (-) operator. It subtracts corresponding elements from list2 from list1 and creates a new list with the results. For example, zip(list1, list2) pairs up the elements at corresponding indices from list1 and list2. The for loop iterates over each pair of elements and subtracts list2_i from list1_i, and appends the result to the sub_list.
# Empty list
sub_list = []
# List subtraction
# Using zip() and “-” operator
zip_object = zip(list1, list2)
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append(list1_i – list2_i)
print(“After subtracting the list:”, sub_list)
Yields the same output as above.
6. Using operator.sub() Method
Similarly, you can do list subtraction using the operator.sub() method. This method subtracts the corresponding elements from list2 from list1 and creates a new list with the results.
In the below example, operator.sub is a function from the operator module that performs subtraction. The map() function applies the operator.sub function to each pair of elements from list1 and list2, creating an iterable of the results. Finally, the list() function is used to convert the iterable into a list. The resulting list sub_list contains the differences [1, 4, -1, 7, -5], which are the results of subtracting list2 from list1 element-wise.
import operator
# Using operator.sub() method
sub_list = list(map(operator.sub, list1, list2))
print(“After subtracting the list:”, sub_list)
Yields the same output as above.
7. List Subtraction Using enumerate() Function
You can also compute list subtraction using the enumerate() function. The enumerate() function allows iterating over a list while simultaneously accessing both the index and the corresponding element. For example, enumerate(list1) pairs up the elements list1 with their corresponding indices. The list comprehension iterates over each pair, accessing the index i and the element as an element. It subtracts the element at the index i in list2 from the element and creates a new list sub_list with the results.
# Using enumerate() function
sub_list = [element – list2[i] for i, element in enumerate(list1)]
print(“After subtracting the list:”, sub_list)
Yields the same output as above.
8. Using For Loop
You can also compute list subtraction by iterating over the indices of the lists and subtracting the corresponding elements from list2 from list1. The differences are then appended to the sub_list using a for loop.
In the below example, for loop iterates over the range of indices from 0 to the length of list1. Inside the loop, it subtracts the element at the index i in list2 from the element at the same index i in list1. The result is stored in the element variable and then appended to the sub_list using the append() method.
# Using for loop
sub_list = []
for i in range(len(list1)):
element = list1[i] – list2[i]
sub_list.append(element)
print(“After subtracting the list:”, sub_list)
Yields the same output as above.
9. Using np.subtract() Method
Finally, you can use the NumPy library to perform list subtraction. By converting the input lists into NumPy arrays, you can leverage the np.subtract() function to subtract the corresponding elements. The resulting array is then converted back into a list.
import numpy as np
# Initialization lists
list1 = [2, 8, 5, 9, 3]
list2 = [1, 4, 6, 2, 8]
# Using np.subtract() method
array1 = np.array(list1)
array2 = np.array(list2)
sub_array = np.subtract(array1, array2)
sub_list = list(sub_array)
print(“After subtracting the list:”, sub_list)
Yields the same output as above.
Conclusion
In this article, I have explained Python list subtraction by using the zip(), list comprehension, lambda, map(), – operator, operator.sub(), enumerate(), for loop, and np.subtract() functions with examples.
Happy Learning !!
How to subtract lists in Python? List subtraction involves subtracting the corresponding elements from one list to another, producing a new list with the differences between each pair of elements at the same index. This operation is indeed similar to performing subtraction in mathematics. To perform list subtraction, the two input lists must have the Read More Python, Python Tutorial, Pending review by admin, Python List Examples