0 votes
1 view
in Data Science by (18.3k points)

I have a list of tuples like

(A, 1), (B, 2), (C, 3), (A, 9), (B, 8).

How to get the mean of each value referred to the first element of the tuples, not knowing the number of occurrences of the first element of tuples?

I want something like:

(A, 5), (B, 5), (C, 3).

1 Answer

0 votes
by (36.8k points)

Using groupby and itemgetter:

from itertools import groupby

from operator import itemgetter

from statistics import mean

s = [('A', 1), ('B', 2), ('C', 3), ('A', 9), ('B', 8)]

s2 = sorted(s, key=itemgetter(0))   # sorting the tuple based on 0th index

print([(k, int(mean(list(zip(*g))[1]))) for k, g in groupby(s2, itemgetter(0))])


[('A', 5), ('B', 5), ('C', 3)]

 If you are a beginner and want to know more about Data Science the do check out the Data Science course

Welcome to Intellipaat Community. Get your technical queries answered by top developers !