Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing dupes in list of lists in Python

Basically, I'm trying to do remove any lists that begin with the same value. For example, two of the below begin with the number 1:

a = [[1,2],[1,0],[2,4],[3,5]]

Because the value 1 exists at the start of two of the lists -- I need to remove both so that the new list becomes:

b = [[2,4],[3,5]]

How can I do this?

I've tried the below, but the output is: [[1, 2], [2, 4], [3, 5]]

def unique_by_first_n(n, coll):
    seen = set()
    for item in coll:
        compare = tuple(item[:n])
        print compare   # Keep only the first `n` elements in the set
        if compare not in seen:
            seen.add(compare)
            yield item

a = [[1,2],[1,0],[2,4],[3,5]]

filtered_list = list(unique_by_first_n(1, a))
like image 587
Jamie Lunn Avatar asked Aug 26 '18 19:08

Jamie Lunn


1 Answers

An efficient solution would be to create a Counter object to hold the occurrences of the first elements, and then filter the sub-lists in the main list:

from collections import Counter
counts = Counter(l[0] for l in a)
filtered = [l for l in a if counts[l[0]] == 1]
#[[2, 4], [3, 5]]
like image 70
Joe Iddon Avatar answered Oct 06 '22 03:10

Joe Iddon