Remove common elements from two list in Python Last Updated : 17 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. Python a = [1, 2, 3, 4, 5] b = [4, 5, 6, 7, 8] # Using set operations to remove common elements result1 = list(set(a) - set(b)) result2 = list(set(b) - set(a)) # Combine both results c = result1 + result2 print(c) Output[1, 2, 3, 8, 6, 7] Other methods that can remove common elements from two lists are:Table of ContentUsing Sets Using List ComprehensionUsing Loop Using collections.CounterCollection.counter to count occurrences of elements in both lists and then subtracts the counters, which gives the elements that are unique to each list. Python from collections import Counter a = [1, 2, 3, 4, 5] b = [4, 5, 6, 7, 8] # Using Counter to count and subtract common elements counter1 = Counter(a) counter2 = Counter(b) # Subtract the counters to remove common elements final_result = list((counter1 - counter2).elements()) + list((counter2 - counter1).elements()) print(final_result) Output[1, 2, 3, 6, 7, 8] Using List ComprehensionList comprehension is a concise and readable way to remove common elements. While it is more intuitive than sets, it can be slower for large lists because it uses the not in operator, which has linear time complexity. Python a = [1, 2, 3, 4, 5] b = [4, 5, 6, 7, 8] # Using list comprehension to filter out common elements a = [item for item in a if item not in b] b = [item for item in b if item not in a] print(a) print(b) Output[1, 2, 3] [4, 5, 6, 7, 8] Each time we use not in in a list, it has to check all items in the other list, making the operation O(n) for each check. This can result in O(n*m) time complexity, where n and m are the sizes of the lists.Using LoopThe loop method is straightforward but not very efficient. It loops over both lists and adds items to new lists if they are not common. This method is less optimal for large lists. Python a = [1, 2, 3, 4, 5] b = [4, 5, 6, 7, 8] # Creating new lists to store the result a_new = [] b_new = [] # Looping through list a and adding only non-common elements to a_new for item in a: if item not in b: a_new.append(item) # Looping through list b and adding only non-common elements to b_new for item in b: if item not in a: b_new.append(item) # Final result after removing common elements a = a_new b = b_new print(a) print(b) Output[1, 2, 3] [6, 7, 8] Comment More infoAdvertise with us Next Article Remove common elements from two list in Python A akashjha2671 Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Python | Remove given element from the list Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9 7 min read Remove Multiple Elements from List in Python In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40, 2 min read Remove Elements From a List Based on Condition in Python In Python, lists are a versatile and widely used data structure. There are often situations where you need to remove elements from a list based on a specific condition. In this article, we will explore five simple and commonly used methods to achieve this task. Remove Elements From A List Based On A 3 min read Remove an Element from a List by Index in Python We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2 3 min read Python | Remove trailing empty elements from given list When working with lists in Python, it's common to encounter lists with extra None elements at the end. These trailing None values can cause issues in our programs . We can remove these trailing None elements using simple methods like slicing, loops, or filter.Using List Slicing Slicing is a very eff 3 min read Like