Vinod Chahar
Vinod Chahar

Reputation: 41

How to combine two lists, remove duplicates, without changing element order of the lists?

I want to combine two lists and remove duplicate values.

Suppose I have two lists:

lst1 = [23,15,46,76,45]
lst2 = [43,32,15,49,45,10]

I want to output:

result = [23,15,46,76,45,43,32,49,10]

In the result, the list order of the elements must be maintained. For example, in the second list, element 43 at position 0, then after that 32 came in, then after that 49 came, then after that 10 came and so on...

Upvotes: 3

Views: 5400

Answers (8)

Craig
Craig

Reputation: 928

I know this is an old question, but nobody has mentioned using dict, which will remove duplicates and, as of Python 3.7, preserve order.

Here are two ways to do it:

lst1 = [23,15,46,76,45]
lst2 = [43,32,15,49,45,10]

print( list({x:0 for x in lst1+lst2}.keys()) )
print( list(dict.fromkeys(lst1+lst2,0).keys()) )

Output:

[23, 15, 46, 76, 45, 43, 32, 49, 10]
[23, 15, 46, 76, 45, 43, 32, 49, 10]

Upvotes: 0

McGrady
McGrady

Reputation: 11477

If you don't need the order of the list, maybe you can try:

set(lst1).union(set(lst2))

And you can get a set {32, 10, 43, 76, 45, 46, 15, 49, 23}.

If you want to keep the order of the list, you can try this:

print(lst1+[i for i in lst2 if i not in lst1])

Output:

[23, 15, 46, 76, 45, 43, 32, 49, 10]

If there may be duplicate elements in lst1 and lst2,example:

lst1 = [1,2,1,3]
lst2 = [2,4,5,4]

And you still want to keep the order of these two lists, you can remove the duplicate elements and then combine them:

ts = set()
rm_duplicate=lambda l:[x for x in l if not (x in ts or ts.add(x))]

print(rm_duplicate(lst1)+[i for i in rm_duplicate(lst2) if i not in lst1])

Result:

[1, 2, 3, 4, 5]

Upvotes: 1

RaminNietzsche
RaminNietzsche

Reputation: 2791

You can use set:

In [1]: set(lst1).union(set(lst2))
Out[1]: {10, 15, 23, 32, 43, 45, 46, 49, 76}

If the order is important you can use this:

resulting_list = lst1
resulting_list.extend(x for x in lst2 if x not in resulting_list)

Output:

[23, 15, 46, 76, 45, 43, 32, 49, 10]

Upvotes: 3

Abhishek Gurjar
Abhishek Gurjar

Reputation: 7476

You can use set like this,

lst1 = [23,15,46,76,45]
lst2 = [43,32,15,49,45,10]

print list(set(lst1 + lst2))

Upvotes: 0

Paul Panzer
Paul Panzer

Reputation: 53029

If your list elements are hashable:

st1 = set(lst1)
result = lst1 + [i for i in lst2 if not i in st1]

In your toy example you won't feel the difference but if the lists are moderately large converting to set will speed up element lookup.

Upvotes: 0

Hendrik F
Hendrik F

Reputation: 3930

Python has a built-in set type that does this type of operation:

set(lst1).union(set(lst2))

Upvotes: -1

John
John

Reputation: 13699

result = lst1 + [element for element in lst2 if element not in lst1]

Upvotes: 1

Batman
Batman

Reputation: 8927

Just add the elements that aren't already in the first list, from the second:

for element in list_2:
    if element not in list_1:
        list_1.append(element)

Upvotes: 2

Related Questions