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 items. Please note that lists are mutable. We can add, remove, and modify the items anytime in a Python program. Here is an example of a Python list:
demo_list1 = ['Mark', 'Alex', 1987, 2020] demo_list2 = [5, 4, 3, 2, 1 ] demo_list3 = ["x", "y", "z"]
To print a list in Python, we need to make use of the print() function in Python. Like this:
print(demo_list1)
Now, let’s talk about the main topic of today: How to subtract two lists in Python. We can determine the difference between two lists in Python mainly in two different ways. Let’s get started.
Using 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 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.
At the end of the program, we’ll simply 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]
I hope this tutorial helped you to understand how to subtract two lists in Python!
Happy coding!
![Marcel Iseli (Maschi)](https://maschituts.com/wp-content/uploads/2020/12/HochzeitFotoKlein.jpg.webp)
Hey guys! It’s me, Marcel, aka Maschi. On MaschiTuts, it’s all about tutorials! No matter the topic of the article, the goal always remains the same: Providing you guys with the most in-depth and helpful tutorials!