Find the value in the middle of the list

Question:

Greeting everyone! I need help, i have the list with an odd numbers of elements, and i have to find the value placed in the middle of the list. I mean, i have the list: [1, 4, 5, 6, 7]
The middle value is 5 because it placed between 2 numbers left and 2 numbers right. So how to find this value with python?

Asked By: rockzxm

||

Answers:

try this:

l = [1,2,3]

l[len(l)//2]
Answered By: baozilaji

you first get the length of your list len(listName) you can store it in a new variable. The element in the middle of the list is the element at the index length // 2 because the first element of the list have an index of 0
so the element will be:
array[len(array) // 2]

Answered By: Mahdi Saïd

To find a middle number, just use array.length/2
but if array.length is even, you may need to find two.
The way to find a middle number is different from problem to problem.
So you should read Question well.

Answered By: dsbikz
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.