for i in range(n):
for j in range(o):
orderedList.append(newStringList[j][i])
I'm a beginner Python programmer
I have an encryption problem to solve, in which I must split a string into chunks of n size, then add the first index of each to a list, then the second index of each, etc. I have a solution to the problem that works when all chunks are equal, however, the last chunk can sometimes be smaller if len(string)%n != 0. When this occurs the above loop I have for adding chars to the list goes out of range and the program doesn't work. Is there any solution for this.
Edit
Seems I was a bit unclear
The encryption takes a string e.g. "123456789" and a number n e.g. 3. Then divides the string into chunks of size n. So newStringList[123,456,789]. I then take the first element from newStringList[0] to newStringList[n-1], then moves onto the second element and so on. So orderedList[1,4,7,2,5,8,3,6,9]. Now you have a better backdrop for the above problem.
Adding one if condition before the last line will fix the issue. The if condition is to make sure that you are accessing the array list only when the element exist.
for i in range(n):
for j in range(o):
if j < len(newStringList) and i < len(newStringList[j]) :
orderedList.append(newStringList[j][i])