Matching / counting lists in python dictionary

I have a dictionary {x: [a,b,c,d], y: [a,c,g,f,h],...}. Thus, a key is a single variable with a value being a list (of different sizes).

My goal is to compare each list with each list in the dictionary and return with a count of the number of repetitions of a particular list.

I tried this but it doesn't seem to work:

count_dict = {}
counter = 1
for value in dict.values():
  count_dict[dict.key] = counter
  counter += 1
+4
source share
1 answer

You can map lists to tuples so that they can be used as keys and use Counterdict to count:

from collections import Counter 

count = Counter(map(tuple, d.values()))
+7
source

Source: https://habr.com/ru/post/1616447/


All Articles