By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,773 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,773 IT Pros & Developers. It's quick & easy.

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

P: 1
how to get the middle number when,
1.the list have even number of values
2.the list have odd number of values
Nov 11 '12 #1
Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
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
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 11 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.