/  Technology   /  Combining two lists and removing duplicates

Combining two lists and removing duplicates

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.

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.

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.

 

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)

 

 

 

Leave a comment