user10432374
user10432374

Reputation:

How to remove the duplicates from a list

A string array F1 has got names of Facebook users and their association if U1, U2 it imples that U1 is a friend of U2. This alos implies that U2 is a friend of U1 So, read F1 and remove duplicates and write all the unique paris of F2

test_list = ["U1,U2","U3,U4","U2,U1"]

res_list = [] 
for i in range(len(test_list)): 
    if test_list[i] not in test_list[i + 1:]: 
        res_list.append(test_list[i]) 
print ("Resultant list is : " + str(res_list)) 

original Output:

   Resultant list is : ['U1,U2', 'U3,U4', 'U2,U1']

expected output:

     Resultant list is : ['U1,U2', 'U3,U4']

Upvotes: 0

Views: 396

Answers (1)

Mark
Mark

Reputation: 92430

You could split the values into a frozenset and make a set of those. This will remove the duplicates. Then you can rejoin back to strings. You need to use frozenset because regular sets are not hashable and you can't make a set from them.

test_list = ["U1,U2","U3,U4","U2,U1"]
unique = set([frozenset(s.split(",")) for s in test_list])
[",".join(s) for s in unique]

Result:

['U2,U1', 'U4,U3']

This doesn't necessarily preserve the order of the original strings. Since the sets are symmetrical it probably doesn't matter. But if it does you can make a slightly less-compact function to actually filter through the list:

def dedupe(l):
    seen = set()
    res = []
    for s in l:
        fs = frozenset(s.split(','))
        if fs not in seen:
            res.append(s)
        seen.add(fs)
    return res

This will preserve the first string found that's new:

['U1,U2', 'U3,U4']

Upvotes: 2

Related Questions