" ", dict fromkeys, :
li=['A', 'a', 'z']
s = "Another test string with x and y but no capital h."
def count(s, li):
cnt={}.fromkeys(li, 0)
for c in s:
if c in cnt:
cnt[c]=cnt[c]+1
return cnt
>>> count(s, li)
{'A': 1, 'a': 3, 'z': 0}
, , , :
def count(s, li):
cnt={}.fromkeys(li, 0)
for c in (e for e in s if e in cnt):
cnt[c]+=1
return cnt
, Pythonic - :
>>> from collections import Counter
>>> c=Counter(s)
>>> c
Counter({' ': 10, 't': 7, 'n': 4, 'h': 3, 'i': 3, 'a': 3, 'o': 2, 'e': 2, 'r': 2, 's': 2, 'A': 1, 'g': 1, 'w': 1, 'x': 1, 'd': 1, 'y': 1, 'b': 1, 'u': 1, 'c': 1, 'p': 1, 'l': 1, '.': 1})
dict :
>>> {k:c[k] for k in li}
{'A': 1, 'a': 3, 'z': 0}