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?
Answers:
try this:
l = [1,2,3]
l[len(l)//2]
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]
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.
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?
try this:
l = [1,2,3]
l[len(l)//2]
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]
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.