Python list index out of range error

'''
nd = input().split()
n = int(nd[0])

d = int(nd[1])

expenditure = list(map(int, input().rstrip().split()))

def median(a):
a.sort()
if len(a) %2 ==0:
med = (a[len(a)//2 - 1] + a[len(a)//2])/2
return med
else :
med = a[len(a)//2]
return med
count = 0
for i in range(d):
z = median(expenditure[i:d])
if expenditure[i+d] > 2z :
count = count + 1
print(count)
''''
The input which I am giving is :
9 5
2 3 4 2 3 6 8 4 5
This code shows list index out of range error at this line : if expenditure[i+d] > 2
z .
I need help. I am having problems when I use for loop to access a elements of a list using its index(when index has some calculation to perform Like here i +d .) Please help.

Comments (1)