How to get unique values from list in specific order?

Ger Cas :

I have a list with repeated values like below:

L1 = ['r-A','r-G','S1','r-A','S2','r-O','r-G','S2','S1','r-A']

And like to remove repeated elements containing 'r-' leaving the first value only in the corresponding position.

This is:

For 'r-A' select L1[0] and in output would be L2[0]
For 'r-G' select L1[1] and in output would be L2[1]
For 'r-O' select L1[5] and in output would be L2[4]

So the output list would be:

L2 = ['r-A','r-G','S1','S2','r-O','S2','S1']

Using set() I get the unique values but it shows them in different order and removes values that don´t contain r-

>>> list(set(L1))
['S2', 'S1', 'r-A', 'r-G', 'r-O']

How can I do this?

Jack Fleeting :

This should do it:

L1 = ['r-A','r-G','S1','r-A','S2','r-O','r-G','S2','S1','r-A']
L2 = []
for item in L1:
    if 'r-' in item and item  in L2:
         continue
    else:
        L2.append(item)
print(L2)

Output:

['r-A', 'r-G', 'S1', 'S2', 'r-O', 'S2', 'S1']

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=13773&siteId=1