Comparing previous and next values in a list or in a dataframe
<p>I am new here and have, after a lot of research, not been able to crack this one.</p>

<p>My List looks somewhat like this:</p>

<pre><code>lister=["AB1","AB2","AB3","AB3-2","AB3-3","AB3-4","AB4","AB4-2","AB5"]
</code></pre>

<p>It is a list of existing folders and cannot be changed into something more practical.
I also have this list as a pandas df column along with some other values.</p>

<p>The goal is for elements which have a "-2", "-3", "-#" to only use the element which has the biggest value. These "-#" values can go up to 10.</p>

<p>A Result from the list above would be:</p>

<pre><code>resulter=["AB1","AB2","AB3-4","AB4-2","AB5]
</code></pre>

<p>Thanks a lot for the help!</p>

<p><strong>UPDATE:</strong></p>

<p>The answer from John Zwinck is working for the lists. However, when I try to use it on a pandas dataframe it gives me errors. So to reframe my question would possible be more helpful:</p>

<p>My Dataframe looks like this:</p>

<pre><code>   COL1 COL2    COL3   COL4     COL5      COL6
0   1    77      AB1  0.609856  2.145556  2.115333                                                                                                                                    
1   2    77      AB2  0.603378  2.146333  2.125667                                                                                                                                    
2   3    77      AB3  0.600580  2.150667  2.135000                                                                                                                                   
3   4    89      AB1  0.609129  2.149056  2.097667                                                                                                                                  
4   5    89      AB2  0.604061  2.175333  2.142667                                                                                                                                  
5   6    89      AB3  0.606987  2.139944  2.107333                                                                                                                                  
6   7    89      AB4  0.603696  2.122000  2.102000                                                                                                                                 
7   8    94      AB1  0.606438  2.156444  2.142000                                                                                                                                  
8   9    94    AB1-2  0.611260  2.133556  2.095000                                                                                                                                    
9  10    94      AB2  0.596059  2.169056  2.137333 
</code></pre>

<p>My requirement in this case is to remove the row 7 based on the value of COL3 (AB1) because there exists an AB1-2 value in row 8. </p>

<p>Thanks again!</p>

<p>this is <strong>not best</strong> answer and i think has <strong>bad performance</strong> but if someone need pure python without any module or use Cython(typed variables) this may help:</p>

<pre><code>lister=["AB1","AB2","AB3","AB3-2","AB3-3","AB3-4","AB4","AB4-2","AB5"]

resulter = list()
i=0
while i&lt; len(lister)-1:
    if '-' not in lister[i] and '-' not in lister[i+1]:
        resulter.append(lister[i])
    elif '-' not in lister[i] and '-' in lister[i+1]:
        j=i+1
        tmp = lister[j]
        while '-' in tmp and j&lt;len(lister)-1 and lister[i][2] == lister[j+1][2]:
            j += 1
            tmp = lister[j]
        i=j
        resulter.append(tmp)
    i+=1
if lister[-1] not in resulter:
    resulter.append(lister[-1])
print(resulter)
</code></pre>

python string list pandas