Q:

Get mean of values for each tuple in a list in the format (string, value) in Python

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 occurences of the first element of tuples?
I want something like:
(A, 5), (B, 5), (C, 3).

A:

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)]

