python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

python

hey guys i'm currently taking a python course in codecademy and i encountered a difficult question. Create a function called middle_element that has one parameter named lst. If there are an odd number of elements in lst, the function should return the middle element. If there are an even number of elements, the function should return the average of the middle two elements. i decided to use the modulus operator this is how far in writing the code i got: def middle_element(lst): number_of_elements = len(lst) if number_of_elements % 2 == 0 return whats disturbing me is how to write the function that will take the middle number from number_of _elements. since there is no way for me to know how many elements it will have hence making selecting from the list a bit tricky. please assist.

1st Apr 2020, 4:23 PM
Jamal Jillo
Jamal Jillo - avatar
12 Answers
+ 4
Perhaps you can try using the integer division operator (//). For example, 5//2==2 (instead of a float 2.5). When length is even, the average of lst[len(lst)//2] and lst[(len(lst)//2)-1] might be what you're looking for.
1st Apr 2020, 4:31 PM
Stan Suo
Stan Suo - avatar
+ 3
Yep, thanks. I of course meant 3//2, not 3//1. If the length is odd, length//2 will be the index of the middle element.
1st Apr 2020, 5:16 PM
Russ
Russ - avatar
+ 2
Not sure who downvoted StanS 's answer but I'm sure it's on the right line at least. If lst has 3 elements, 3//2 returns 1, so lst[1] will return the middle element. For even-length lists, lst[len(lst)//2] will return the second of the two middle elements.
1st Apr 2020, 4:55 PM
Russ
Russ - avatar
+ 2
Thanks a lot, Russ. :). I've edited my previous answer to correct the mistake and avoid confusion. Jamal Jillo , apologies for not making it clear in the first place, as I thought you were asking how to get the middle number when number_of_elements % 2 ==0. As Russ replied, if the length is odd, length//2 would be the index of the middle element. If the length is even, length//2 would be the index of the 'second' of the two middle elements.
1st Apr 2020, 6:14 PM
Stan Suo
Stan Suo - avatar
+ 1
Jamal Jillo I think it doesn't matter. you can for example ask the user to input a list of numbers. since the input function returns a string, you can use the split method to get list of the numbers in that string. use this for example to get the list of numbers : lst = (float(x) for x in inputed_str. slpit()) for the rest I think that StanS got it.
1st Apr 2020, 5:09 PM
John Robotane
John Robotane - avatar
+ 1
Russ 3//1 would return 3 so lst[3] would return the 3rd element which is the last element
1st Apr 2020, 5:11 PM
Jamal Jillo
Jamal Jillo - avatar
+ 1
for example if you use this instruction in_str=input() and the user input this: 13 10 17 19 the input method will return a string "13 10 17 19" to the in_str variable. calling the string split method on it splits the string according to the given delimiter. in_str.split(" ") will return a list ["13","10","17","19"]. the statement I gave you in my previous post will convert those numbers string to float or int if you want.
1st Apr 2020, 5:22 PM
John Robotane
John Robotane - avatar
0
i used the modulus operator '%' since i want to make sure that i can find out if the number of elements is odd. because say number_of_elements=6 then number_of-elements % 2 will bring 0 hence confirming the first if statement. so now i need a function that will select the middle two elements before i can write one for finding the average.
1st Apr 2020, 4:53 PM
Jamal Jillo
Jamal Jillo - avatar
0
John Robotane please explain the split method further
1st Apr 2020, 5:14 PM
Jamal Jillo
Jamal Jillo - avatar
0
okay thanks let me work on it
1st Apr 2020, 5:43 PM
Jamal Jillo
Jamal Jillo - avatar
- 1
Hii
3rd Apr 2020, 12:07 PM
Pawan Kumar Saini
Pawan Kumar Saini - avatar