how to get the middle value of a list in python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ashen
    New Member
    • Nov 2012
    • 1

    how to get the middle value of a list in python?

    how to get the middle number when,
    1.the list have even number of values
    2.the list have odd number of values
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Are you looking for the median? There are two middle numbers when there is an even number of values.

    The median is the middle number of a sorted list of numbers. If the list has an even number of values, the median is calculated as the average of the two middle numbers.

    The middle number of a list named "s" with an odd number of values is returned by
    Code:
    s[len(s)/2]
    I will leave it to you to determine the other condition. Hint: You can use the modulo operator (%) to determine if the list has an even or odd number of values.

    Comment

    Working...