Write Python Program to Count Values Associated with Key in Dictionary

Write a python program to count the values associated with key in a dictionary is provided on this page.

Write A Python Program To Count The Values Associated With Key In A Dictionary.

In this program we count the unique values associated with a key in list of dictionaries.


dictionary = [
{'id': 1, 'success': True, 'name': 'John'},
{'id': 2, 'success': True, 'name': 'Cena'},
{'id': 3, 'success': False, 'name': 'Rock'}
]


print(len(list(set([data['id'] for data in dictionary]))))
print(len(list(set([data['success'] for data in dictionary]))))
print(len(list(set([data['name'] for data in dictionary]))))

Output


3
2
3

The output is simple, first we got 3 because there are 3 unique values for key 'id'.

Then for key 'success' we got 2 and for key 'name' we got 3 since there are three unique values.

Related To Python

Comments