I suppose you could use the Counter
's update
method to add 0 to all of the keys in the unique list
>>> from collections import Counter
>>> x = Counter(['abc','def','abc'])
>>> y = ['abc', 'def', 'abc', 'abc', 'ghi', 'jkl','jkl']
>>> x.update(dict.fromkeys(y, 0))
So that x
now contains the keys and values you want.
Or, if you want to avoid the construction of the intermediate dict
, the slightly longer:
>>> from collections import Counter
>>> x = Counter(['abc','def','abc'])
>>> y = ['abc', 'def', 'abc', 'abc', 'ghi', 'jkl','jkl']
>>> for k in y:
x[k] += 0