Bilal
Bilal

Reputation: 61

Matching/Counting lists in python dictionary

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

My goal is to match up each list against every list in the dictionary and come back with a count of how many times a certain list has been repeated.

I tried this but does not seem to work:

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

Upvotes: 4

Views: 44

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

You could map the lists to tuples so they can be used as keys and use a Counter dict to do the counting:

from collections import Counter 

count = Counter(map(tuple, d.values()))

Upvotes: 7

Related Questions