How to access the middle value of a list separated by commas

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Justin Ziegler

    How to access the middle value of a list separated by commas

    [3,4,5]

    and i want to assign these values to variables, but i want to assign the number with the middle VALUE, how do i go about doing that?
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    See Section 1.1.5 at this onine book.

    Comment

    • Justin Ziegler
      New Member
      • Oct 2010
      • 3

      #3
      that didn't really help :/... if i already assigned 2 of the numbers can i assign it by the only unassigned element in the list?

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        You should at least read it, before asking someone else to do it for you. Verbatim copy:
        l = ['a', 'b', 1, 'a cat']
        and access it with indices, exactly as you did with character strings:
        >>> l[1]
        'b'

        Comment

        • Justin Ziegler
          New Member
          • Oct 2010
          • 3

          #5
          yes yes, i know that but the problem was that the list is user inputed and it is subject to change so that wont work and besides i read it all, it doesn't even cover all the function possibilities of a list.

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Do you mean the median of the list? Eg if your list were [5,3,4] would you want to assign a variable to 4, or to 3? I think the question is ambiguous. Perhaps you can try to be more clear about exactly what it is that you want.

            Is the issue that the list might have a variable number of elements? Or that you want to assign to the median element? Or that you want to assign to an element that has not yet been used for something?

            Help us to help you!

            Comment

            • Justin Ziegler
              New Member
              • Oct 2010
              • 3

              #7
              yes,It was a question about returning the median of a list of only 3 elements that the user enters, heres a snippet of my code if it helps, i need B to represent the median of the list.

              [side_1=input('E nter side one: ')
              side_2=input('E nter side two: ')
              side_3=input('E nter side three: ')
              values=[side_1,side_2,s ide_3]
              A=min(values)
              B=
              C=max(values)]

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                The median of any list is the middle value in a sorted list.
                Code:
                >>> values = [3,8,2,9,6]
                >>> sorted(values)
                [2, 3, 6, 8, 9]
                >>> sorted(values)[len(values)/2]
                6
                >>>
                It's a bit more complicated when the list contains an even number of elements.

                Comment

                • Glenton
                  Recognized Expert Contributor
                  • Nov 2008
                  • 391

                  #9
                  bvdet's method should work fine for you, Justin. Although, if you're using python 3, if I recall it won't do the floor division, so instead use:
                  Code:
                  B=sorted(values)[(len(values)-1)/2]
                  or, since you know the length already, just use:
                  Code:
                  B=sorted(values)[1]

                  Comment

                  Working...