Search code examples
pythonlistdictionarycounter

Extracting duplicate values from a defaultdict


I have a defaultdict defined as follows:

devicedict1=collections.defaultdict(list)

The device dict contains an index key, and double values as follows:

{0: ['9085', '9084'], 1: ['9084', '5684'], 2: ['9084', '3707'], 3: ['9084', '3707'], 4: ['3707', '9084'], 5: ['9084', '3707'], 6: ['3707', '9084'], 7: ['9084', '5684'], 8: ['9084', '3707'], 9: ['9084', '3707'], 10: ['9084', '3707'], 11: ['9084', '5684'], 12: ['3707', '9084']

I want to extract the tuples (value, value) that appear the most, and print them.

I've tried using Counter and items().

e=Counter(devicedict1.items())
print(e)

but it doesn't seem to work. It should give us the following results:

['9084', '3707']:30
['9084', '5684']:10

Solution

  • You can use Counter if you map the values in the dictionary to tuples, given that unlike lists, tuples are hashable. To get the n tuples that appear the most there is the most_common method:

    n = 3
    Counter(map(tuple, devicedict1.values())).most_common(n)
    # [(('9084', '3707'), 6), (('9084', '5684'), 3), (('3707', '9084'), n)]