Finding multiple repetitions of smaller lists in a large cumulative list
<p>I have a large list of strings, for eg:</p>

<pre><code>full_log = ['AB21','BG54','HG89','NS72','Error','CF54','SD62','KK02','FE34']
</code></pre>

<p>and multiple small list of strings, for eg:</p>

<pre><code>tc1 = ['HG89','NS72']
tc2 = ['AB21','BG54']
tc3 = ['KK02','FE34']
tc4 = ['CF54','SD62']
</code></pre>

<p>I want to find each of this smaller lists in the larger list maintaining the sequence, so that the output would be something like:</p>

<pre><code>tc2-tc1-Er-tc4-tc3
</code></pre>

<p>I want to know if there is any straight forward, pythonic way of dealing with this situation.</p>

<p>You need to create a map (dictionary) of the elements of your small list:</p>

<pre><code>m = {k: v for k, v in zip(map(tuple, [tc1, tc2, tc3, tc4])), ["tc1", "tc2", "tc3", "tc4"])}
&gt;&gt;&gt; {('KK02', 'FE34'): 'tc3', ('AB21', 'BG54'): 'tc2', ('CF54', 'SD62'): 'tc4', ('HG89', 'NS72'): 'tc1'}
</code></pre>

<p>You could then use an iterator to loop over the list:</p>

<pre class="lang-python prettyprint-override"><code>itr = iter(full_log)

for i in itr:
    if i != "Error":
        n = next(itr)
        if n != "Error":
            if (i, n) in m:
                print m[(i, n)]
        else:
            print "Er"
    else:
        print "Er"



&gt;&gt;&gt; tc2
    tc1
    Er
    tc4
    tc3
</code></pre>

<p>If you dont mind expanding your "Error" entries in the first list:</p>

<pre><code>full_log2 = [item for sublist in [[i] if i != "Error" else ["Error", "Error"] for i in full_log] for item in sublist]
&gt;&gt;&gt; ['AB21', 'BG54', 'HG89', 'NS72', 'Error', 'Error', 'CF54', 'SD62', 'KK02', 'FE34']
</code></pre>

<p>Then you could use a list comprehension:</p>

<pre><code>print [m[(full_log2[i], full_log2[i+1])] if (full_log2[i], full_log2[i+1]) in m else "Er" for i in range(0, len(full_log2)-1, 2)]
&gt;&gt;&gt; ['tc2', 'tc1', 'Er', 'tc4', 'tc3']
</code></pre>

python list