Write in python a list num having the following elements: num = [3 ,21 ,5 ,6 ,14 ,8 ,14 ,3] . Write a program to swap the content with the next value divisible by 7 so that the resultant list looks like: num = [3, 5, 21, 6, 8, 14, 3, 14]
This one is my code...
num = [3, 21, 5, 6, 14, 8, 14, 3]
for i in range(len(num)-1):
if num[i] % 7 == 0:
num[i + 1], num[i] = num[i], num[i + 1]
print(num)
output...
[3, 5, 6, 14, 8, 14, 3, 21]
After the first swap, the if statement checks 21 again and again, and it gets pushed to the end of the list in contrast with the desired output. I have to check other elements too that are divisible by 7 and swap them with the succeeding element. Can I skip one iteration after each swap?