Merge Two Lists Without Duplicates - Python Last Updated : 08 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given two lists containing elements and our task is to merge them into a single list while ensuring there are no duplicate values. For example: a = [1, 2, 3, 4] and b = [3, 4, 5, 6] then the merged list should be: [1, 2, 3, 4, 5, 6]Using set()set() function automatically removes duplicates making it the most efficient way to merge two lists while keeping only unique elements. Python a = [1, 2, 3, 4] b = [3, 4, 5, 6] res = list(set(a + b)) print(res) Output[1, 2, 3, 4, 5, 6] Explanation:a + b merges both lists and set(a + b) removes duplicates.list(set(...)) converts the set back into a list.Using Dictionary Keys (dict.fromkeys())Since dictionary keys are unique, we can use dict.fromkeys() to remove duplicates while preserving the order of elements. Python a = [1, 2, 3, 4] b = [3, 4, 5, 6] res = list(dict.fromkeys(a + b)) print(res) Output[1, 2, 3, 4, 5, 6] Explanation:a + b merges both lists and dict.fromkeys(a + b) creates a dictionary where keys are the merged elements (removing duplicates).list(...) converts the dictionary keys back into a list preserving order.Using a for LoopWe can manually iterate through both lists and add elements to a new list only if they are not already present. Python a = [1, 2, 3, 4] b = [3, 4, 5, 6] res = [] for x in a + b: if x not in res: res.append(x) print(res) Output[1, 2, 3, 4, 5, 6] Explanation:We initialize an empty list result and iterate through a + b, check if x is already in result.If x is not in result then only we append it.Using List Comprehension with setThis method combines a set for fast lookups with a list comprehension to preserve order efficiently. Python a = [1, 2, 3, 4] b = [3, 4, 5, 6] seen = set() res = [x for x in a + b if x not in seen and not seen.add(x)] print(res) Output[1, 2, 3, 4, 5, 6] Explanation:We initialize an empty set() called seen to track unique elements.We iterate through a + b using list comprehension and if x is not in seen then it is added to result, seen.add(x) adds x to seen ensuring duplicates are ignored. Comment More infoAdvertise with us Next Article Python | Merge two lists alternatively H harshasi1eet Follow Improve Article Tags : Python Python Programs Python list-programs python Practice Tags : pythonpython Similar Reads Python - Merging duplicates to list of list We are given a list we need to merge the duplicate to lists of list. For example a list a=[1,2,2,3,4,4,4,5] we need to merge all the duplicates in lists so that the output should be [[2,2],[4,4,4]]. This can be done by using multiple functions like defaultdict from collections and Counter and variou 3 min read Python - Merge Dictionaries List with duplicate Keys Given two List of dictionaries with possible duplicate keys, write a Python program to perform merge. Examples: Input : test_list1 = [{"gfg" : 1, "best" : 4}, {"geeks" : 10, "good" : 15}, {"love" : "gfg"}], test_list2 = [{"gfg" : 6}, {"better" : 3, "for" : 10, "geeks" : 1}, {"gfg" : 10}] Output : [{ 4 min read Python | Merge two lists alternatively Given two lists, write a Python program to merge the given lists in an alternative fashion, provided that the two lists are of equal length. Examples: Input : lst1 = [1, 2, 3] lst2 = ['a', 'b', 'c'] Output : [1, 'a', 2, 'b', 3, 'c'] Input : lst1 = ['name', 'alice', 'bob'] lst2 = ['marks', 87, 56] Ou 4 min read Comparing Python Lists Without Order Sometimes we need to compare two lists without worrying about the order of elements. This is particularly useful when checking if two lists contain the same elements regardless of their arrangement. In this article, we'll explore different ways to compare Python lists without considering order.Using 3 min read Python Merge Dictionaries with Same Keys When working with dictionaries in Python, you should understand that they are mutable, unordered collections of key-value pairs. This flexibility allows us to quickly merge dictionaries, but what happens when two dictionaries have the same key? Python allows us to manage this situation. In this arti 3 min read Python | Remove duplicates from nested list The task of removing duplicates many times in the recent past, but sometimes when we deal with the complex data structure, in those cases we need different techniques to handle this type of problem. Let's discuss certain ways in which this task can be achieved. Method #1 : Using sorted() + set()Â Th 5 min read Like