How to combine two lists while removing duplicates in the new list and keeping duplicates in original list in Python

Combining two lists while removing duplicates in the new list and keeping duplicates in original list returns a list of containing all of the values in the first list and all unique values in the second that are not in the first. For example, combining [1, 2, 2, 3,] and [3, 4] with these conditions will return [1, 2, 2, 3, 4].

Solution for How to combine two lists while removing duplicates in the new list and keeping duplicates in original list in Python : You can use set() and list() to combine two lists while removing duplicates in the new list and keeping duplicates in original list Call set(list_1) and set(list_2) to generate sets of the elements in list_1 and list_2 respectively which contain no duplicates.

Call list(set_2 – set_1) to generate a list of elements in list_2 that are not in list_1.

Add the original list, list_1, to the result of the previous step, list_2_items_not_in_list_1 to return a combined list.


how-to-combine-two-lists-while-removing-duplicates-in-the-new-list-and-keeping-duplicates-in-original-list-in-python