Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two lists without duplicate values

list1 = ["palani", "samy","be"]
list2 = ["palani", "samys","be"]

def find_common(list1,list2):
    for x in list1:
      for y in list2:
        if x == y :
          list2.remove(x) 


    print" unique string 1:",list1
    print" unique string 2:",list2
    print" combained string 2:",list1.append(list2)


find_common(list1,list2)

enter image description here

Why am I getting None?

like image 593
Palani P Avatar asked Sep 19 '18 10:09

Palani P


People also ask

How do I join two lists without duplicates?

Python merges two lists without duplicates could be accomplished by using a set. And use the + operator to merge it.

How do I combine two lists in Excel without duplicates?

You can also merge lists without duplicates in Google Sheets. Select and right-click a second range that will be merged (e.g., C2:C6) and click Copy (or use the keyboard shortcut CTRL + C).

How do you combine two lists in Excel?

On the Data tab, under Tools, click Consolidate. In the Function box, click the function that you want Excel to use to consolidate the data. In each source sheet, select your data, and then click Add.

How to combine lists without duplicates in Excel?

Excel Combine List without Duplicates 1 Copy one of the two lists and paste it to the bottom of the other list, select the new list, then click 2 In the 3 Then a dialog pops up to tell you how many unique values have been selected, click 4 And copy the selected unique values and paste them to a new column ... See More....

How to merge two lists in Excel?

How to Merge Two Lists in Excel 1 Copy one of the two lists and paste it to the bottom of the other list. 2 Select the list and click 3 In the 4 Then a dialog displays on the screen to tell you the duplicates have been delete, click See More....

How to combine two arrays without duplicate values in C #?

How to combine two arrays without duplicate values in C#? Learn how to combine two arrays without duplicate values in C# using the Union () method. In the same way, use the Union () method with the number array. If an array contains objects of a custom class, then you need to implement IEquatable<T> or IEqualityComparer<T>, as shown below.

How do I remove duplicates from a list?

To delete the duplicates, click anywhere in the first list (B2:B15), and in the Ribbon go to Data > Remove Duplicates. 4. In the pop-up window, uncheck the second column and click OK.


2 Answers

This could be accomplished by using set:

a = ['hello', 'world']
b = ['hello', 'universe']

unique = list(set(a + b))

print(unique)

# ['universe', 'hello', 'world']

Note: this won't work for a list of dictionaries!

like image 183
Nebulastic Avatar answered Oct 13 '22 22:10

Nebulastic


import numpy as np

np.unique(list1+list2) # keeps only non dublicates

this is also keeps the order incase that was a priority

like image 36
Jonas Wolff Avatar answered Oct 13 '22 23:10

Jonas Wolff