Q: Calculate the average of lists in Python I want to calculate the average (mean) of the items in a list within a list.  The result should be a list of tuples:
Input_1 = [[2,4,6], [4,8,1]]            # ==> [(4,), (4.333,)]
Input_2 = [2,6], [8,6], [1,5], [4,5,1]  # ==> [(4,), (7,), (3,), (3.333,)]


A: Here is a simple solution using list comprehensions
from statistics import mean

Output_1 = [(mean(l),) for l in Input_1]

Another thing you can do if you don't want to use statistics library is
Output_1 = [(sum(l)/len(l),) for l in Input_1]


A: Below is a simple method that will do this task
# importing mean() 
from statistics import mean 

def Average(Input):
    Output =[]                   # Initialising a blank Output List
    for x in Input:
        a = round(mean(x),3)     # Rounding the mean value to 3 decimal digits
        t= (a,)                  # Making tuple with Mean
        Output.append(t)         # Making the list of Mean tuples
    return Output

Input_1=[[2,4,6],[4,8,1]]
Input_2=[2,6],[8,6],[1,5],[4,5,1]
print(Average(Input_1))
print(Average(Input_2))


A: You can use the higher-order function map (Python inherited it from LISP) which applies a function to all the members of an iterable (such as a list):
from statistics import mean

Input_1 = [[2,4,6], [4,8,1]]
Input_2 = [2,6], [8,6], [1,5], [4,5,1]

def mean_in_tuple(it):
    return (mean(it), )

for it in [Input_1, Input_2]:
    result = list(map(mean_in_tuple, it))
    print(result)

At first sight, it's not that different from a list comprehension.  But it can lead to a completely different style of coding called functional programming.  This style is usually easier to reason about and test.  It also composes well into pipelines:
map(format_euro, map(dollar_to_euro, map(mean_in_tuple, input)))

