Python Practice › Tuple Programs › Program 32
Frequency of tuple elements
Write a Python program to count how often each element appears in a tuple.
Input
("a", "b", "c", "d", "b", "a", "b")Output
{'a': 2, 'b': 3, 'c': 1, 'd': 1}Approach
Counter.
Solution
from collections import Counterprint(dict(Counter(("a", "b", "c", "d", "b", "a", "b"))))Output
{'a': 2, 'b': 3, 'c': 1, 'd': 1}Learn the concept
New to this topic? Read the lesson, then come back.