0
def outlier(*args):
    outlist=[]
    def median(args1):
        if(len(args1)%2==1):
            return list(sorted(args1))[int((len(args1)/2)-0.5)]
        else:
            return (list(sorted(args1))[int(len(args1)/2)]+list(sorted(args1))[int(len(args1)/2)-1])/2
    def fmax(args2):
        sortargs=sorted(args2)
        return sortargs[-1]
    def fmin(args3):
        sortargs=sorted(args3)
        return sortargs[0]
    q1=median(list(range(fmin(args),floor(median(args))+1)))
    q3=median(list(range(floor(median(args)),fmax(args)+1)))
    for i in args:
        if(i<(q1-1.5*(q3-q1)) or i>(q3+1.5*(q3-q1)*(q3-q1))):
            outlist.append(i)
    return outlist

print(outlier(1,2,3,4,5,6,7,8,9,10,100000000))

I have tried to get the outlier values of a list in Python , but everytime I try it ,it returns an empty list or throws an error.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 1
    Note that Python provides a `min` and a `max` function that are much more efficient than sorting the list and taking the first or last value. Also, you should avoid long lines stuffed with nested function calls. Do one thing at a time, and in case of problem, it will be much easier to just check (simply print it) the value of your intermediate variables. – Thierry Lathuille Nov 21 '21 at 08:49

1 Answers1

1

If the list returns empty, the reason is that neither parts of your if condition is met and so nothing is appended to the list:

if(i<(q1-1.5*(q3-q1)) or i>(q3+1.5*(q3-q1)*(q3-q1))):   # never met

If you are not adverse to useing bigger module, you could calculate the quartiles using numpy, see

or use this answer that gives you a function for manual calculation:


BTW:

  • sorted() returns a list so list(sorted(..)) is redundant
  • while smaller functions are nice, multiple times sorting the same data is not efficient - sort it once - to get the min/max of a list use:
  • all of your calculations need sorted input - you may as well sort it once on top and use the sorted list as inputs further down

You can also get the min and max in one sorting:

def minmax(data):
    if len(data) < 2: 
        raise ValueError("Must be iterable of len 2 or more")
    srt = sorted(data)

    return srt[0], srt[-1]

instead of

def fmax(args2):
    sortargs=sorted(args2)
    return sortargs[-1]
def fmin(args3):
    sortargs=sorted(args3)
    return sortargs[0]
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thanks for your helps ,but ı wanted to do it without libraries .If ı wanted to solve my own problem by using libraries, there is in Pandas data.describe() and it gives you the both of quartiles. If ı wanted to do it by using libraries ,ı could use that ,but that doesn't mean you didn't help me ;contrariwise, you have helped me very much by leting me know some new attributes of pandas that ı didn't know until the time ı read your answer for the question ı ask . I can be accepted new in the data science ,ı have started 3 months ago, so you can understand why ı don't know these methods. –  Nov 21 '21 at 09:46
  • 1
    As ı see my median func doesn't return a list ,it returns either int or float. –  Nov 21 '21 at 09:48