Python find and replace last appearance in list
<p>In Python, I have a list of list</p>

<pre><code>list3 = ['PA0', 'PA1']
list2 = ['PB0', 'PB1']
list1 = ['PC0', 'PC1', 'PC2']

[(list1[i], list2[j], list3[k]) for i in xrange(len(list1)) for j in xrange(len(list2)) for k in xrange(len(list3))]

#Result
[('PC0', 'PB0', 'PA0'), 
('PC0', 'PB0', 'PA1'), 
('PC0', 'PB1', 'PA0'), 
('PC0', 'PB1', 'PA1'), 
('PC1', 'PB0', 'PA0'), 
('PC1', 'PB0', 'PA1'), 
('PC1', 'PB1', 'PA0'), 
('PC1', 'PB1', 'PA1'), 
('PC2', 'PB0', 'PA0'), 
('PC2', 'PB0', 'PA1'), 
('PC2', 'PB1', 'PA0'), 
('PC2', 'PB1', 'PA1')]
</code></pre>

<p>How can I find the <strong>last appearance</strong> and add <strong>E</strong> as suffix</p>

<pre>
[('PC0', 'PB0', 'PA0'), 
 ('PC0', 'PB0', 'PA1'), 
 ('PC0', 'PB1', 'PA0'), 
 (<b>'PC0E'</b>, 'PB1', 'PA1'), 
 ('PC1', 'PB0', 'PA0'), 
 ('PC1', 'PB0', 'PA1'), 
 ('PC1', 'PB1', 'PA0'), 
 (<b>'PC1E'</b>, 'PB1', 'PA1'), 
 ('PC2', 'PB0', 'PA0'), 
 ('PC2', <b>'PB0E'</b>, 'PA1'), 
 ('PC2', 'PB1', <b>'PA0E'</b>), 
 (<b>'PC2E'</b>, <b>'PB1E'</b>, <b>'PA1E'</b>)]
</pre>

<p>Process your input list in reverse, then mark the <strong>first</strong> occurrence of any value. You can use a list of sets to track what values you've already seen. Reverse the output list you build when you are done:</p>

<pre><code>seensets = [set() for _ in inputlist[0]]
outputlist = []
for entry in reversed(inputlist):
    newentry = []
    for value, seen in zip(entry, seensets):
        newentry.append(value + 'E' if value not in seen else value)
        seen.add(value)
    outputlist.append(tuple(newentry))
outputlist.reverse()
</code></pre>

<p>Demo:</p>

<pre><code>&gt;&gt;&gt; seensets = [set() for _ in inputlist[0]]
&gt;&gt;&gt; outputlist = []
&gt;&gt;&gt; for entry in reversed(inputlist):
...     newentry = []
...     for value, seen in zip(entry, seensets):
...         newentry.append(value + 'E' if value not in seen else value)
...         seen.add(value)
...     outputlist.append(tuple(newentry))
...
&gt;&gt;&gt; outputlist.reverse()
&gt;&gt;&gt; pprint(outputlist)
[('PC0', 'PB0', 'PA0'),
 ('PC0', 'PB0', 'PA1'),
 ('PC0', 'PB1', 'PA0'),
 ('PC0E', 'PB1', 'PA1'),
 ('PC1', 'PB0', 'PA0'),
 ('PC1', 'PB0', 'PA1'),
 ('PC1', 'PB1', 'PA0'),
 ('PC1E', 'PB1', 'PA1'),
 ('PC2', 'PB0', 'PA0'),
 ('PC2', 'PB0E', 'PA1'),
 ('PC2', 'PB1', 'PA0E'),
 ('PC2E', 'PB1E', 'PA1E')]
</code></pre>

python list