vanjavk
vanjavk

Reputation: 117

What is an easy way to remove duplicates from only part of the string in Python?

I have a list of strings that goes like this:

1;213;164
2;213;164
3;213;164
4;213;164
5;213;164
6;213;164
7;213;164
8;213;164
9;145;112
10;145;112
11;145;112
12;145;112
13;145;112
14;145;112
15;145;112
16;145;112
17;145;112
1001;1;151
1002;2;81
1003;3;171
1004;4;31

I would like to remove all duplicates where second 2 numbers are the same. So after running it through program I would get something like this:

1;213;164
9;145;112
1001;1;151
1002;2;81
1003;3;171
1004;4;31

But something like

8;213;164
15;145;112
1001;1;151
1002;2;81
1003;3;171
1004;4;31

would also be correct.

Upvotes: 0

Views: 54

Answers (3)

One Lyner
One Lyner

Reputation: 2004

Here is a nice and fast trick you can use (assuming l is your list):

list({ s.split(';', 1)[1] : s for s in l }.values())

No need to import anything, and fast as can be.

In general you can define:

def custom_unique(L, keyfunc):
    return list({ keyfunc(li): li for li in L }.values())

Upvotes: 1

Tom Ron
Tom Ron

Reputation: 6181

You can group the items by this key and then use the first item in each group (assuming l is your list).

import itertools
keyfunc = lambda x: x.split(";", 1)[1]
[next(g) for k, g in itertools.groupby(sorted(l, key=keyfunc), keyfunc)]

Upvotes: 1

snatchysquid
snatchysquid

Reputation: 1352

Here is a code on the few first items, just switch my list with yours:

x = [
'7;213;164',
'8;213;164',
'9;145;112',
'10;145;112',
'11;145;112',
]
new_list = []
for i in x:
    check = True
    s_part = i[i.find(';'):]
    for j in new_list:
        if s_part in j:
            check = False
    if check == True:
        new_list.append(i)

print(new_list)

Output:

['7;213;164', '9;145;112']

Upvotes: 0

Related Questions