It seems to be counting so defaultdict could be an option:
>>> from collections import defaultdict >>> c = defaultdict(int) >>> rules = [1, 2, 3, 4, 2, 3, 5, 4] >>> for rule in rules: ... c[rule] += 1 ... >>> c defaultdict(int, {1: 1, 2: 2, 3: 2, 4: 2, 5: 1}) >>> c[3] 2Or Counter:
>>> from collections import Counter >>> c = Counter(rules) >>> c Counter({1: 1, 2: 2, 3: 2, 4: 2, 5: 1}) >>> c[5] += 1 >>> c[5] 2EDIT: regarding 2 code: elif will be never executed therefore redundant. First if catches all True conditions and there is nothing left for elif not to mention that somewhere must be loop from which to break out (not provided in code sample).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.