Skip to content Skip to sidebar Skip to footer

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',

Solution 1:

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

Solution 2:

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]

Solution 3:

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

Post a Comment for "Preserving The Order In Difference Between Two Lists"