TransWikia.com

List of tuples remove duplicates by first index

Stack Overflow Asked by Rankinstudio on January 26, 2021

I’m having a hard time with this one. I have a list of tuples but am only interested in removing duplicates based on the first item of the tuples.

I need to remove the duplicates from the individual lists by only looking at the first item in the tuple.

I need to compare the lists for duplicates where unique is in uniqueFound and only keep those from unique that are not in uniqueFound, also only interested in the first item of the tuple. The other numbers and values should not be considered. Here is what I came up with, and it’s still passing N1 to the final list when it shouldn’t. There has to be an easier way to do this?

#unique found
uniqueFound = [('N1', '20.3', 1, 58, 14),
         ('N2', '20.1', 1, 44, 14),
         ('N3', '21', 1, 23, 14),
         ('N1', '21', 1, 23, 14),
         ('N5', '33', 1, 34, 14)
         ]
unique = [('N0', '23', 2, 22, 1),
         ('N6', '33.1', 1, 25, 11),
         ('N3', '21', 1, 33, 12),
         ('N1', '44', 1, 2, 44),
         ('N7', '33.2', 4, 22, 12),
         ('N7', '3.2', 2, 21, 132)
         ]

#Remove duplicates
uniqueFound = dict((x[0], x) for x in uniqueFound).values()
unique = dict((x[0], x) for x in unique).values()

fmatches=[]
uniquefinal = []

for x in uniqueFound:
    for y in unique:
        if x[0] == y[0]:
            fmatches.append(y)
        else:
            continue
fmatches = dict((x[0], x) for x in fmatches).values()
for x in unique:
    for y in fmatches:
        if x[0] == y[0]:
            continue
        else:
            uniquefinal.append(x)
duplicatesrem = dict((x[0], x) for x in uniquefinal).values()
uniqueFinal = list(duplicatesrem)
print(uniqueFinal)

which yields

[('N0', '23', 2, 22, 1), ('N6', '33.1', 1, 25, 11), ('N3', '21', 1, 33, 12), ('N1', '44', 1, 2, 44), ('N7', '3.2', 2, 21, 132)]

What I’m going for (in unique but not in uniqueFound):

[('N0', '23', 2, 22, 1), ('N6', '33.1', 1, 25, 11), ('N7', '3.2', 2, 21, 132)]

Thanks for your help.

3 Answers

uniqueFound = [('N1', '20.3', 1, 58, 14),
         ('N2', '20.1', 1, 44, 14),
         ('N3', '21', 1, 23, 14),
         ('N1', '21', 1, 23, 14),
         ('N5', '33', 1, 34, 14)
         ]
unique = [('N0', '23', 2, 22, 1),
         ('N6', '33.1', 1, 25, 11),
         ('N3', '21', 1, 33, 12),
         ('N1', '44', 1, 2, 44),
         ('N7', '33.2', 4, 22, 12),
         ('N7', '3.2', 2, 23, 1)
         ]

aux1 = set(e[0] for e in uniqueFound)
aux2 = set()
result = []
for e in unique:
    e0 = e[0]
    if e0 in aux1: continue
    if e0 in aux2: continue
    aux2.add(e0)
    result.append(e)
print(result)

aux1 is an auxiliary set containing "keys" from uniqueFound.

aux2 is an auxiliary set containing already used "keys"

if a "key" (e0) is found in aux1 or aux2, do nothing. Otherwise add "key" to aux2 and the element to the result

Correct answer by Jan Stránský on January 26, 2021

Another solution that utilizes dictionaries:

found = {i[0] for i in uniqueFound}
dct = {k: v for k, *v in unique if k not in found}
[(k, *v) for k, v in dct.items()]
# [('N0', '23', 2, 22, 1), ('N6', '33.1', 1, 25, 11), ('N7', '3.2', 2, 23, 1)]

Answered by Mykola Zotko on January 26, 2021

drop = {t[0] for t in uniqueFound} # set comprehension

# build a conditional list comprehension
uniqueFinal = [e for e in unique if e[0] not in drop]

Answered by RichieV on January 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP