Skip to Content

How to Subtract Two Lists in Python

How to Subtract Two Lists in Python

Python is powerful yet simple and easy to grab for beginners compared to other programming languages. The sequence is the basic data structure in Python.

Each element of a sequence is assigned a number – its position or index. In a sequence, the first index is 0; the second index is 1, and so on.

Python has six in-built sequential data structures. Among them all, the lists, tuple, and array is widely used in Python. This article will discuss the Python lists and how to subtract two lists in Python.

Before approaching further, let’s make sure we have a good understanding of Python lists. People often messed up in between arrays and lists. For now, let’s agree Python lists are like arrays though there is a tiny difference among them.

Lists are the most versatile and dynamic datatypes in Python. We can store different types of data inside a list.

It doesn’t matter if they are numbers, characters, or strings. To initialize a list, we must put all the values inside a square bracket and differentiate each value using a comma.

Elements of a list are known as the item. Please, note that lists are mutable. We can add, remove, modify the items anytime in a Python program.  Here is an example of the Python list:

 

demo_list1 = ['Mark', 'Alex', 1987, 2020]

demo_list2 = [5, 4, 3, 2, 1 ]

demo_list3 = ["x", "y", "z"]

 

To print a list, we just need to write the print() function in Python. Like this way:

 

print(demo_list1)

 

Now, let’s one more step ahead to reach our goal-subtract two lists in Python. We can determine the difference between the two lists mainly in two different ways. Let’s get started.

 

Using the set()

Set is a collection-type object which can store elements of different data types. A set() doesn’t index the values in a particular order. This method converts the lists into the python sets explicitly and then finds the difference between them. Here is a sample program is given for your better understanding.

 

def list_diff(my_list1, my_list2):

    return (list(set(my_list1) - set(my_list2)))

my_list1 = [10, 16, 21, 26, 31, 36, 41]

my_list2 = [10, 26, 41, 36]
print(list_diff(my_list1, my_list2))

 

Output: [16, 21, 31]

The set() function accepted the defined two lists in the above program and turned them out into sets. Inside the print() function, it just calculated the difference.

 

Using a nested for-loop

To calculate the subtract value between two different lists, we can simply use a nested for-loop.

In this method, we’ll compare all the second list items with the first one sequentially, and while traversing, we’ll be appending every non-matching item to a new empty list.

End of the program, we’ll print the list. Here you go:

 

def list_diff(my_list1, my_list2):

    out = []

    for ele in my_list1:

    	if not ele in my_list2:

        	out.append(ele)

    return out

my_list1 = [10, 16, 21, 26, 31, 36, 41]

my_list2 = [10, 26, 41, 36]

print(list_diff(my_list1, my_list2))

 

Output: [16, 21, 31]

 

Using the list comprehension method

List comprehension is the same method as the nested for-loop. Here we will replace the for-loop with the list comprehension syntax. For instance,

 

def list_diff(my_list1, my_list2):

    out = [item for item in my_list1 if not item in my_list2]

    return out

my_list1 = [10, 16, 21, 26, 31, 36, 41]

my_list2 = [10, 26, 41, 36]

print(list_diff(my_list1, my_list2))

 

Output: [16, 21, 31]

 

We hope that after wrapping up this tutorial, you should know several ways to subtract two Python lists. However, you may practice more with examples to gain confidence.

How to Center Text in Photoshop
Previous
How to Center Text in Photoshop - Step by Step
HOW TO USE CSS IN PHP ECHO
Next
HOW TO USE CSS IN PHP ECHO