def count(x,A): c = 0 for n in A: if n == x: c += 1 return c nums = [] with open("numbers.txt") as f: for line in f.readlines(): x = int(line) nums.append(x) counts = [] for n in range(1,21): counts.append((n,count(n,nums))) with open("counts.txt","w") as f: for n,c in counts: f.write("{} {}\n".format(n,c))