How would I turn this into dictionary comprehension?
dict_ = defaultdict(int)
for sequence in set_sequence:
for ele in sequence:
dict_[ele] += 1
How would I turn this into dictionary comprehension?
dict_ = defaultdict(int)
for sequence in set_sequence:
for ele in sequence:
dict_[ele] += 1
You can use nested comprehension, with Counter
:
from collections import Counter
set_sequence = [['a','b','b'], ['b', 'c', 'c']]
dict_ = Counter(ele for sequence in set_sequence for ele in sequence)
print(dict_) # Counter({'b': 3, 'c': 2, 'a': 1})