Ssank
Ssank

Reputation: 3667

Preserving the order in difference between two lists

I have two lists l and l_match. l_match is an empty list.

l = ['gtttaattgagttgtcatatgttaataacg',
     'tttaattgagttgtcatatgttaataacgg',
     'ttaattgagttgtcatatgttaataacggt',
     'taattgagttgtcatatgttaataacggta',
     'aattgagttgtcatatgttaataacggtat']

l_match = []

print list(set(l) - set(l_match))

gives the output

['aattgagttgtcatatgttaataacggtat',
 'tttaattgagttgtcatatgttaataacgg',
 'ttaattgagttgtcatatgttaataacggt',
 'taattgagttgtcatatgttaataacggta',
 'gtttaattgagttgtcatatgttaataacg']

I want the output the same order as the input. i.e. in the above case the output should be

['gtttaattgagttgtcatatgttaataacg',
 'tttaattgagttgtcatatgttaataacgg',
 'ttaattgagttgtcatatgttaataacggt',
 'taattgagttgtcatatgttaataacggta',
 'aattgagttgtcatatgttaataacggtat']

Can you suggest edits?

Upvotes: 4

Views: 5120

Answers (5)

Pepv
Pepv

Reputation: 171

Try using filter():

result = list(filter(lambda c: c not in l_match, l))

Upvotes: 0

Gabriel Pena
Gabriel Pena

Reputation: 439

This is old af but, in case someone is still wondering about it, a little googling gave me this really simple solution.

x = [1, 2, 6, 8, 2, 3]
y = [2, 6]
sorted(set(x) - set(y), key=x.index)

output -> [1, 8, 3]

Upvotes: 2

Edwin Torres
Edwin Torres

Reputation: 2864

What about this: How do you remove duplicates from a list in whilst preserving order?

l = ['gtttaattgagttgtcatatgttaataacg', 'tttaattgagttgtcatatgttaataacgg', 'ttaattgagttgtcatatgttaataacggt', 'taattgagttgtcatatgttaataacggta', 'aattgagttgtcatatgttaataacggtat']
seen = set()
seen_add = seen.add
print([ x for x in l if not (x in seen or seen_add(x))])

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180481

Just make l_match a set:

l_match = []

st =  set(l_match)

print([ele for ele in l if ele not in st])

If l can have dupes use an OrderedDict to get unique values from l:

from collections import OrderedDict
print([ele for ele in OrderedDict.fromkeys(l) if ele not in st])

Obviously l_match would contain values in the real world or a simple l[:] = OrderedDict.fromkeys(l) would suffice to remove dupes from l and keep the order

Upvotes: 3

Robin James Kerrison
Robin James Kerrison

Reputation: 1767

You should look through l and include each element therein in your result array only if it's not in l_match. This will preserve the order. In python, the statement is a single line:

print [entry for entry in l if entry not in l_match]

Upvotes: 0

Related Questions