The output that you want looks slightly strange due to the inconsistency between the two cases. You could trivially change this example to get whatever output you want, however:
lists = [
[u'a', 11, u'P'],
[u'a', 11, u'A'],
[u'b', 2, u'P'],
[u'c', 1, u'P'],
[u'c', 2, u'P'],
[u'd', 1, u'P'],
[u'e', 3, u'P'],
[u'f', 2, u'P'],
[u'a', 1, u'P'],
[u'a', 2, u'P'],
[u'b', 1, u'P'],
[u'b', 11, u'P']]
# Each key in this dictionary will be one of the first elements
# from the lists shown above. The values will be dictionaries
# mapping a letter (one of the third elements in each list) to
# their total count (i.e. the sum of the second elements matching
# the other two columns)
from collections import defaultdict
results = defaultdict(dict)
for main_key, count, subkey in lists:
d = results[main_key]
d[subkey] = d.get(subkey,0) + count
for main_key, values in results.items():
print main_key, "=>", values
The output is:
a => {u'A': 11, u'P': 14}
c => {u'P': 3}
b => {u'P': 14}
e => {u'P': 3}
d => {u'P': 1}
f => {u'P': 2}
Update: Thanks to sharjeel for suggesting in the comment below that I remove the setdefault by using defaultdict instead.
Update 2: In your further question in the comments below, you indicate that instead you want as output "[a] set of lists like [[u'a', 11, u'P'], [u'a', 11, u'A']". (I'm assuming for the moment that you mean a list of lists rather than a set, but that's almost as easy.) In order to construct such a list of lists , you could replace the loop that prints the values with the following:
lists_output = []
for main_key, values in results.items():
for subkey, count in values.items():
lists_output.append([main_key,count,subkey])
print lists_output
... which will give the output:
[[u'a', 11, u'A'], [u'a', 14, u'P'], [u'c', 3, u'P'], [u'b', 14, u'P'], [u'e', 3, u'P'],
[u'd', 1, u'P'], [u'f', 2, u'P']]