Quantcast
Channel: 'List index out of range' while reversing list - Stack Overflow
Viewing all articles
Browse latest Browse all 5

'List index out of range' while reversing list

$
0
0

The problem is regarding reversing a list A of size N in groups of K. For example if A = [1,2,3,4,5], k = 3Output = [3,2,1,5,4]The error I get, when I run this is List Index out of range on line 4.

def reverseInGroups(A,N,K):       arr1 = []    for i in range(K):        arr1.append(A[(N-i)%K]) #line 4    for j in range(N-K):        arr1.append(A[N-j-1])    return arr1

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images