1

How would I turn this into dictionary comprehension?

dict_ = defaultdict(int)
 for sequence in set_sequence:
  for ele in sequence:
   dict_[ele] += 1
Cornelius
  • 11
  • 1
  • https://stackoverflow.com/a/37585628/14237276 is one. the other is to do update calls inside [] and discard the list – Abel Mar 02 '22 at 01:56

1 Answers1

0

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})
j1-lee
  • 13,764
  • 3
  • 14
  • 26