Find min, max, and average of a list

I am having hard time to figure out how to find min from a list
for example

somelist = [1,12,2,53,23,6,17]

how can I find min and max of this list with defining (def) a function

I do not want to use built-in function min

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

from __future__ import division

somelist =  [1,12,2,53,23,6,17] 
max_value = max(somelist)
min_value = min(somelist)
avg_value = 0 if len(somelist) == 0 else sum(somelist)/len(somelist)

If you want to manually find the minimum as a function:

somelist =  [1,12,2,53,23,6,17] 

def my_min_function(somelist):
    min_value = None
    for value in somelist:
        if not min_value:
            min_value = value
        elif value < min_value:
            min_value = value
    return min_value

Python 3.4 introduced the statistics package, which provides mean and additional stats:

from statistics import mean, median

somelist =  [1,12,2,53,23,6,17]
avg_value = mean(somelist)
median_value = median(somelist)

Method 2

Return min and max value in tuple:

def side_values(num_list):
    results_list = sorted(num_list)
    return results_list[0], results_list[-1]


somelist = side_values([1,12,2,53,23,6,17])
print(somelist)

Method 3

Only a teacher would ask you to do something silly like this.
You could provide an expected answer. Or a unique solution, while the rest of the class will be (yawn) the same…

from operator import lt, gt
def ultimate (l,op,c=1,u=0):
    try:
        if op(l[c],l[u]): 
            u = c
        c += 1
        return ultimate(l,op,c,u)
    except IndexError:
        return l[u]
def minimum (l):
    return ultimate(l,lt)
def maximum (l):
    return ultimate(l,gt)

The solution is simple. Use this to set yourself apart from obvious choices.

Method 4

list=[]
n = int(input("Enter the length of your list: "))
for i in range (1,n+1):
    a=int(input("Enter the %d number: " %i ))
    list.append(a)
min=list[0]
max=list[0]
for i in range(1,n):
    if max<list[i]:
        max=list[i]
    if min>list[i]:
        min=list[i]
print(" %d is the biggest number " %max)
print(" %d is the smallest number " %min)

Method 5

Given your list : somelist = [1,12,2,53,23,6,17], first, make it a DataFrame using Pandas.
Then you can use the describe() method on the DF.

Code:

import pandas as pd
somelist = [1,12,2,53,23,6,17]
somelist_df = pd.DataFrame(somelist)
somelist_df.describe()

OUTPUT:

count   7.000000
mean    16.285714
std 18.052833
min 1.000000
25% 4.000000
50% 12.000000
75% 20.000000
max 53.000000


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x