To sum the content of a sequence, just use sum
. If you want to add all the numbers in the list, you should not, of course, reduce it to a set using set
.
Already, to count the number of occurrences of each element in a sequence (such as a list) or an iterable (like the rows an open file), you can use collections.Counter - which automatically groups the occurrences into an object that can be read as if it were a dictionary:
In [3]: from collections import Counter
In [4]: L = [0, 0, 1, 1, 1]
In [5]: Counter(L)
Out[5]: Counter({0: 2, 1: 3})
In [6]: Counter(L)[1]
Out[6]: 3
Check out the Counter documentation at:
link