Mixalis Mixalis - 3 months ago 9
Python Question

Extract all single {key:value} pairs from dictionary

I have a dictionary which maps some keys to 1 or more values.

In order to map to more than 1 value, I'm mapping each individual key to a list. How can I get the number of the single pairs? Is there a quick pythonic way to do this?

My dict looks something like this:

>>print dict
{'key1':['value11',value12, ...], 'key2': ['value21'], 'key3':['value31', 'value32']}


So in the above example, I would expect my output to be
1

Answer

Iterate through values in dictionary and count:

count  = 0
for value in dic.values():
    if len(value) == 1:
        count += 1

print count
Comments