import collections import csv a = collections.defaultdict(set) with open('test.txt', 'rb') as f: for i, j in csv.reader(f, delimiter=';'): a[i].update([j]) for i in a: a[i] = len(a[i]) print a
А как бы так сделать, чтобы подсчитывались не только уникальные номера, а вообще всё что есть?