Python Practice › Tuple Programs › Program 32

Frequency of tuple elements

EasyTuple Programs · 32 of 70

Write a Python program to count how often each element appears in a tuple.

Advertisement
Input
("a", "b", "c", "d", "b", "a", "b")
Output
{'a': 2, 'b': 3, 'c': 1, 'd': 1}

Approach

  1. Counter.

Solution

Python
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.

More tuple programs

← All 70 Tuple Programs

Leave a Comment