Cory - 1 year ago 52

Python Question

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`

`new_lst`

Example:

`new_lst = [4,3,8,8]`

`a`

`b`

`(4 + 8) / 2`

`a`

`new_lst`

Answer Source

You don't want the `list.index`

function - this is for finding the position of an item in a list. To find the item at a position, you should use list indexing (I suppose that the naming is what confused you), which looks like `lst[index]`

.

```
>>> new_lst = [4, 3, 8, 8]
>>> new_lst.sort()
>>> new_lst
[3, 4, 8, 8]
>>> if len(new_lst) % 2 == 0:
a = new_lst[len(new_lst)//2-1]
b = new_lst[len(new_lst)//2]
print((a+b)/2)
6.0
```