Write code to calculate the average of: {3, 11, 78, 112, 4, 18} in one single line of code


#1

{3, 11, 78, 112, 4, 18} in one single line of code.


#2

sum([3,11,78,112,4,18])/6

If it has to work with arbitrary lists of number I don’t think it can be done with a single line of code, since we need to put the list in a variable:

my_list = [3,11,78,112,4,18]
print sum(my_list)/len(my_list)

#3

you could do:

sum([3,11,78,112,4,18])/len([3,11,78,112,4,18])

but @jochum solution is absolute preferable. His calculation is still within a single line


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.