how to count duplicates in a list of tuples and append it as a new value

Question:

output = [('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'),('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'),('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'),('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'),('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'),('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO')]

so here there are total of 6 set of ('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO') in this above list

so Im expecting an output like this ~

expected_output = [('studentA','ISDF',6), ('studentB','CSE',6),('studentC','BIO',6)]

The format should be [('student', 'department', total count)]

Asked By: Shad

||

Answers:

Try this

sorted([tuple(list(item)+[output.count(item)]) for item in set(output)])

or

sorted([item+(output.count(item),) for item in set(output)])
Answered By: Mostafa Ayaz

You could use Counter:

from collections import Counter

output = [('studentA', 'ISDF'), ('studentB', 'CSE'), ('studentC', 'BIO'),
          ('studentA', 'ISDF'), ('studentB', 'CSE'), ('studentC', 'BIO'),
          ('studentA', 'ISDF'), ('studentB', 'CSE'), ('studentC', 'BIO'),
          ('studentA', 'ISDF'), ('studentB', 'CSE'), ('studentC', 'BIO'),
          ('studentA', 'ISDF'), ('studentB', 'CSE'), ('studentC', 'BIO'),
          ('studentA', 'ISDF'), ('studentB', 'CSE'), ('studentC', 'BIO')]

counts = Counter(output)
print(counts)
print([k + (v, ) for k, v in counts.items()])

Out:

Counter({('studentA', 'ISDF'): 6, ('studentB', 'CSE'): 6, ('studentC', 'BIO'): 6})
[('studentA', 'ISDF', 6), ('studentB', 'CSE', 6), ('studentC', 'BIO', 6)]
Answered By: Maurice Meyer

Using Counter() from collections and a list comprehension

from collections import Counter

count = Counter(output)
exp_output = [(key[0], key[1], value) for key, value in count.items()]
print(exp_output)

[('studentA', 'ISDF', 6), ('studentB', 'CSE', 6), ('studentC', 'BIO', 6)]
Answered By: Jamiu S.
output = [('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'), 
    ('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'), 
    ('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'), 
    ('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'), 
    ('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO'), 
    ('studentA','ISDF'), ('studentB','CSE'),('studentC','BIO')]
keys = list(set(output))
[k + (output.count(k),) for k in keys]
Answered By: Andrei Boyanov
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.