Skip to content

Python: Subtract Two Lists (4 Easy Ways!)

Python Subtract Two Lists Cover Image

In this tutorial, you’ll learn how to use Python to subtract two lists. You’ll learn how to do this using the popular data science library numpy, for-loops, list comprehensions, and the helpful built-in zip() function.

Being able to work with lists is an important skill for any Python developer, whether a beginner or an advanced Pythonista. Subtracting two lists is less intuitive than simply subtracting them. Because of this, check out the four different methods provided below to find easy ways to subtract two lists!

The Quick Answer: Use Numpy subtract()

Quick Answer - Python Subtract Two Lists
Subtract two Python lists with the zip() function

Use Numpy to Subtract Two Python Lists

The popular numpy library is often used for working in data science, and, as such, comes bundled with a ton of different helpful methods to manipulate numerical data. One of the primary advantages that numpy provides is the array object, which is very similar to the Python list object.

One of the methods that numpy provides is the subtract() method. The method takes two numpy arrays as input and provides element-wise subtractions between the two lists.

Let’s see how we can use numpy and Python to subtract two lists:

# Subtract two lists with numpy
import numpy as np

list1 = [10, 11, 12]
list2 = [1, 2, 3]

array1 = np.array(list1)
array2 = np.array(list2)

subtracted_array = np.subtract(array1, array2)
subtracted = list(subtracted_array)

print(subtracted)

# Returns: [9, 9, 9]

Let’s take a look at what we’ve done here:

  1. We loaded our two lists, list1 and list2 and converted them into numpy arrays, array1 and array2
  2. We then used the np.subtract() method to subtract the two lists
  3. Finally, since we want to return a Python list, we used the list() method

In the next section, you’ll learn how to use the zip() function to find the different between two lists element-wise.

Use Zip to Subtract Two Python Lists

The Python zip() function is a built-in utility that makes working with different iterable objects incredibly easy. What the function does is iterate over different objects, element by element. Because of this, the zip() function is the ideal candidate for finding the different between two lists element by element.

Let’s take a look at how we can use the Python zip() function to subtract two lists:

# Subtract two lists with for loops
list1 = [10, 11, 12]
list2 = [1, 2, 3]

subtracted = list()
for item1, item2 in zip(list1, list2):
    item = item1 - item2
    subtracted.append(item)

print(subtracted)

# Returns: [9, 9, 9]

Let’s take a look at what we’ve done:

  1. We instantiated a new, empty list called subtracted that we’ll use to hold our subtracted values
  2. We then iterate over two items returned during each iteration of the zip() output.
  3. We assign the difference between these two items to item and append it to our subtracted list

Now, let’s take a look at how to use a for-loop to subtract two lists.

Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.

Python For Loops to Subtract Two Lists

Python for loops are incredibly helpful tools that let us repeat an action for a predetermined number of times. Because of this, we can loop over our two lists and find the different between their items.

Let’s see how we can use for loops to subtract lists:

# Subtract two lists with for loops
list1 = [10, 11, 12]
list2 = [1, 2, 3]

subtracted = list()
for i in range(len(list1)):
    item = list1[i] - list2[i]
    subtracted.append(item)

print(subtracted)

# Returns: [9, 9, 9]

Let’s take a look at what we’ve done with our for-loop method here:

  1. We created an empty list called subtracted
  2. We then loop over the range from 0 through to the length of our first list
  3. We then assign the difference between each ith item of each list to our variable item
  4. This item is then appended to our list

Finally, let’s

Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.

Python List Comprehensions to Subtract Two Lists

Finally, let’s learn how we can use a list comprehension to subtract two lists. This method actually also uses the zip() function to create a new list that subtracts the two lists.

Let’s take a look at how we can do this!

# Subtract two lists with zip()
list1 = [10, 11, 12]
list2 = [1, 2, 3]

subtracted = [element1 - element2 for (element1, element2) in zip(list1, list2)]
print(subtracted)

# Returns: [9, 9, 9]

Let’s take a look at what we’ve done here:

  1. We used a list comprehension to loop over the items in the zip() object of list1 and list2
  2. In this comprehension, we find the difference between the two elements

Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

Conclusion

In this post, you learned how to use to subtract two lists. You learned how to do this using the numpy.subract() method, the zip() function, a Python for-loop, and a Python list comprehension. Working with lists is an important skill that doesn’t always work as you’d expect, so being prepared for different challenges you might encounter is important.

To learn more about the numpy.subtract() method, check out the official documentation.

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

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