1

I have a dictionary object say

d = {
    '25478329': ['17647430', '376088951', '32416061', '43096205'],
    '43096205': '17647430',
    '376088951': ['17647430', '25478329', '32416061'],
    '32416061': ['17647430', '25478329']
}

What I want is to find out the value that has occurred maximum number of times for all keys, e.g. in the above dictionary the answer will be '17647430'.

What I did is:

def di(d):
    a = []
    for i in d.values():
        if isinstance(i,str):
            a.append(i)
        else:
            for j in i:
                a.append(j)
    print a
    most_common,num_most_common = Counter(a).most_common(1)[0]
    print most_common,num_most_common

Can the above code be optimized?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
LearningNinja
  • 437
  • 1
  • 8
  • 23

1 Answers1

3

Chain the values using itertools.chain.from_iterable():

from collections import Counter
from itertools import chain

def di(d):
    counts = Counter(chain.from_iterable(
        [v] if isinstance(v, str) else v for v in d.itervalues()))
    return counts.most_common(1)[0]

Use d.values() instead if this is for Python 3.

You'd be better of if your values were not a mix of lists and strings; I'd make everything lists instead.

Demo:

>>> from collections import Counter
>>> from itertools import chain
>>> def di(d):
...     counts = Counter(chain.from_iterable(
...         [v] if isinstance(v, str) else v for v in d.itervalues()))
...     return counts.most_common(1)[0]
... 
>>> d = {
...     '25478329': ['17647430', '376088951', '32416061', '43096205'],
...     '43096205': '17647430',
...     '376088951': ['17647430', '25478329', '32416061'],
...     '32416061': ['17647430', '25478329']
... }
>>> di(d)
('17647430', 4)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343