Counting the amount of occurrences in a list of tuples

mackwerk picture mackwerk · Apr 15, 2013 · Viewed 41.2k times · Source

I am fairly new to python, but I haven't been able to find a solution to my problem anywhere.

I want to count the occurrences of a string inside a list of tuples.

Here is the list of tuples:

list1 = [
         ('12392', 'some string', 'some other string'),
         ('12392', 'some new string', 'some other string'),
         ('7862', None, 'some other string')
        ]

I've tried this but it just prints 0

for entry in list1:
    print list1.count(entry[0])

As the same ID occurs twice in the list, this should return:

2
1

I also tried to increment a counter for each occurrence of the same ID but couldn't quite grasp how to write it.

*EDIT: Using Eumiro's awesome answer. I just realized that I didn't explain the whole problem. I actually need the total amount of entries which has a value more than 1. But if I try doing:

for name, value in list1:

    if value > 1:
        print value

I get this error:

ValueError: Too many values to unpack

Answer

eumiro picture eumiro · Apr 15, 2013

Maybe collections.Counter could solve your problem:

from collections import Counter
Counter(elem[0] for elem in list1)

returns

Counter({'12392': 2, '7862': 1})

It is fast since it iterates over your list just once. You iterate over entries and then try to get a count of these entries within your list. That cannot be done with .count, but might be done as follows:

for entry in list1:
    print sum(1 for elem in list1 if elem[0] == entry[0])

But seriously, have a look at collections.Counter.

EDIT: I actually need the total amount of entries which has a value more than 1.

You can still use the Counter:

c = Counter(elem[0] for elem in list1)
sum(v for k, v in c.iteritems() if v > 1)

returns 2, i.e. the sum of counts that are higher than 1.