Jamie Lunn
Jamie Lunn

Reputation: 101

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))

Upvotes: 9

Views: 514

Answers (4)

vash_the_stampede
vash_the_stampede

Reputation: 4606

Solution 1

a = [[1,2],[1,0],[2,4],[3,5]]
b = []
for item in a:
    i = 0
    if item[0] == a[i][0]:
        i =+ 1
        continue
    else:
        b.append(item)
        i += 1

Solution 2

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

for item in a:
    for i in range(0, len(a)):
        if item[0] == a[i][0]:
            break
        else:
            if item in b:
                continue
            else:
                b.append(item)

Output

(xenial)vash@localhost:~/pcc/10$ python3 remove_help.py 
[[1, 2], [1, 0], [2, 4], [3, 5]]
[[2, 4], [3, 5]]

Achieved your goal no complex methods involed! Enjoy!

Upvotes: 0

blhsing
blhsing

Reputation: 106445

You can use collections.Counter with list comprehension to get sublists whose first item appears only once:

from collections import Counter
c = Counter(n for n, _ in a)
b = [[x, y] for x, y in a if c[x] == 1]

Upvotes: 5

jpp
jpp

Reputation: 164623

If you are happy to use a 3rd party library, you can use Pandas:

import pandas as pd

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

df = pd.DataFrame(a)
b = df.drop_duplicates(subset=[0], keep=False).values.tolist()

print(b)

[[2, 4], [3, 5]]

The trick is the keep=False argument, described in the docs for pd.DataFrame.drop_duplicates.

Upvotes: 6

Joe Iddon
Joe Iddon

Reputation: 20414

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]]

Upvotes: 6

Related Questions