How to combine two lists and remove duplicates without removing duplicates from first list in Python

1 Answer

0 votes
list_a = [1, 2, 2, 3, 3, 4, 5]
list_b = [2, 3, 4, 8, 9]

first_list_set = set(list_a)
second_list_set = set(list_b)

numbers_only_in_second_list = second_list_set - first_list_set
print(numbers_only_in_second_list)

result_list = list_a + list(numbers_only_in_second_list)

print(result_list)

'''
run:

{8, 9}
[1, 2, 2, 3, 3, 4, 5, 8, 9]

'''

 



answered Nov 15, 2017 by avibootz
...