Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k 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))])

OUTPUT:

[('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

Browse Categories

...