/  Technology   /  Combining two lists and removing duplicates in Python

Combining two lists and removing duplicates in Python

Combining two lists and removing duplicates, without removing duplicates in the original list.

Let us first understand the problem statement by taking an example. We have been given two lists a and b with the given elements.

 

Example:

a = [1, 3, 3, 7]
b = [3, 7, 9, 11]

 

Output:

Now we have to merge these two lists together retaining all the elements in list a and only the unique elements in list b. The merged list should look like this.

 

Example:

result = [1, 3, 3, 7, 9, 11]

 

Output:

We can use two ways to achieve this 

  • Using extend()
  • Using set() and iteration

 

Using extend():

We first consider the list a and create our final list named result. We make use of a for loop and check for the presence of the first list’s elements in the second list, and if the element is not found in the second list, then we append it to the result list using the extend() function.

 

Example:

a = [1, 3, 3, 7]
b = [3, 7, 9, 11]

result = list(a)
result.extend(y for y in b if y not in result)
print(result)

 

Output:

 

Using sort() and iteration:

The set() function returns the unique elements present in a list. So A and B are the lists with unique elements of a and b lists respectively. 

A = set(a)

B = set(b)

We now will find the unique elements only on the second list by subtracting the first unique list A from the second unique list B.

diff_element = B – A

Finally, we add this difference to our first list a.

result = a + list(diff_element)

 

Example:

a = [1, 3, 3, 7]
b = [3, 7, 9, 11]
A = set(a)
B = set(b)

diff_element = B - A

result = a + list(diff_element)
print(result)

 

Output:

 

 

Leave a comment