I'm having trouble with Python here and need your help.
I want to return an item found at a particular index. I can't know what the item is yet, only the index. Everything I have found is the opposite of what I need, i.e., find the index of a known item using myList.index(item)
.
Snippet:
new_lst = x
new_lst.sort()
leng = len(new_lst).....
elif leng > 1 and leng % 2 == 0:
a = (leng / 2) #or float 2.0
b = a - 1
c = new_lst.index(a) #The problem area
d = new_lst.index(b) #The problem area
med = (c + d) / 2.0
return med ......
The above will only return if a
is in new_lst
. Else it errors out. I want to get the middle two numbers (if list is even), add them together and then average them.
Example: new_lst = [4,3,8,8]
. Get em, sort em, then should take the middle two numbers (a
& b
above, indices 1 & 2), add them and average: (4 + 8) / 2
equaling 6. My code would assign 2 to a
, look for it in the list and return an error: 2 not in new_lst
. Not what I want.