24

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

wjandrea
  • 28,235
  • 9
  • 60
  • 81
mtkilic
  • 1,213
  • 1
  • 12
  • 28

5 Answers5

72
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)
monkut
  • 42,176
  • 24
  • 124
  • 155
  • 3
    would like to point out that that average value is susceptible to division by 0 errors. – rtpg Nov 19 '14 at 04:58
  • yes, but I want to find like in array list. What I mean by that is set for min = array[0] then, for i in array, if i =< 0 print min, I need to know how can find min and max this way, I tried but it doesn't work, not sure why – mtkilic Nov 19 '14 at 05:02
  • I'm not sure what you want. Do you want the index of the minimum value? – monkut Nov 19 '14 at 05:06
  • def FindMinimum(array): #write your code here return minimum def FindMaximum(array): #write your code here return maximum def FindAverage(array): #write your code here return average list = [2,4,6,8,1,3,5,7] print "min: ", FindMinimum(list) print "max: ", FindMaximum(list) print "average: ", FindAverage(list) I think this is the one of the example had on codecademy.com i am trying to figure this out at the moment, if you can help me with this I will appreciate so much, Thank you – mtkilic Nov 19 '14 at 05:18
  • Why don't you look into searching/sorting/finding min max/ algorithm. You will get more efficient answers in that community – ajay_t Nov 19 '14 at 05:20
  • 3
    I think it's best if you figure it out and write it yourself. If you have problems understanding please ask. – monkut Nov 19 '14 at 05:23
  • 1
    So 4 x list traversals to get all of those values... Shouldn't there be a one liner for this stuff in python? – niken Mar 21 '17 at 03:20
  • In order to minimize number of comparisons, it is best to proceed in pairs: if a[2k] < a[2k+1]: (if a[2k] < min: min=a[2k]; if max < a[2k+1]: max=a[2k+1]) else: (if a[2k+1] < min: min=a[2k+1]; if max < a[2k]: max=a[2k]) which amounts to 3n/2 comparisons instead of the 2n for the naive implementation; maybe some labda-wizzard can turn that ino a one-liner in python. – user2961818 Jan 01 '21 at 15:27
  • 1
    What for is "from __future__ import division'" there at the top of the code? – Claudio Nov 19 '21 at 21:10
  • 1
    back in 2014 python 2.7 was in common use. The division operator functionality was changed from python 2 to 3, and using `from __future__ import division` essentially imported the python 3 division operator, overriding the python 2 operator functionality. – monkut Nov 20 '21 at 13:14
5

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)
Baldrickk
  • 4,291
  • 1
  • 15
  • 27
2RMalinowski
  • 357
  • 5
  • 3
  • 2
    This is the best way to find the min/max using built-ins. Sorting is O(nlogn), getting min/max is O(1) – Baldrickk Jan 16 '18 at 23:27
  • 9
    @Baldrick: Actually, min() and max() are O(n). So, sorting is slower. – Hinni Sep 26 '18 at 14:52
  • 2
    @Hinni after sorting min is at index `0` and max is at index `length` (or vice versa). Once sorted there is no need to use `min()` or `max()` to find those values. – Baldrickk Oct 01 '18 at 00:22
2

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.

peawormsworth
  • 1,120
  • 9
  • 9
0
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)
-1

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
buddemat
  • 4,552
  • 14
  • 29
  • 49