How to skip the next two iterations in for loop using python?
Question:
In a for loop, let’s assume it contains 100 iterations, and what I need is, i need to print the first element and skip the next two element, and then need to prit the fourth element.. for example,
0 #should print
1 #should skip
2 #should skip
3 #should print
4 #should skip
5 #should skip
6 #should print
.
. like wise
I tried some existing skipping solutions out this platform, but didn’t fit for my problem.
I know the continue statement allows you to skip over the current iteration, but I can’t figure out how to skip the next two iterations. Ttried some itertool fuctions also.
Answers:
If you iterate on your own range
, generate one over three elements
values = ["dog0", "dog1", "dog2", "dog3", "dog4", "dog5", "dog6"]
for i in range(0, 8, 3):
print(i, values[i])
If you iterate over an iterable of values, use enumerate
to filter
values = ["dog0", "dog1", "dog2", "dog3", "dog4", "dog5", "dog6"]
for i, value in enumerate(values):
if i % 3 != 0:
continue
print(i, value)
You can do something like this
arr = [1,2,3,4,5,6,7,8,9,10]
skip_count = 2 # how many to skip
counter = skip_count
for num in arr:
if counter == skip_count:
counter = 0
print(num)
else:
counter += 1
Alternatively, this will work too
arr = [1,2,3,4,5,6,7,8,9,10]
for index in range(0, len(arr), 3):
print(arr[index])
One possible approach would be using enumerate
and mod (%
).
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8]
for index, num in enumerate(nums):
print(num) if index % 3 == 0 else None
If it’s a range you can use the optional step argument range(start, stop, step)
for k in range(start, stop, 3): print(k)
or
for k in range(len(my_data), step=3): print(k)
If it’s a list you can use list slicing
where once again the optional arguments for slicing is start, stop and step!
for k in my_array[::3]: print(k)
I would use enumerate
as others have mentioned. Your problem basically consists in printing all positions [i]
that are multipes of 3
. You can use i%3
to detect when the iteration counter i
is not a multiple of 3:
for i,x in enumerate(???):
if i%3: continue
print(x)
enumerate
also works when you have an iterator f
of unkown size (like when you read a file, etc.)
In a for loop, let’s assume it contains 100 iterations, and what I need is, i need to print the first element and skip the next two element, and then need to prit the fourth element.. for example,
0 #should print
1 #should skip
2 #should skip
3 #should print
4 #should skip
5 #should skip
6 #should print
.
. like wise
I tried some existing skipping solutions out this platform, but didn’t fit for my problem.
I know the continue statement allows you to skip over the current iteration, but I can’t figure out how to skip the next two iterations. Ttried some itertool fuctions also.
If you iterate on your own range
, generate one over three elements
values = ["dog0", "dog1", "dog2", "dog3", "dog4", "dog5", "dog6"]
for i in range(0, 8, 3):
print(i, values[i])
If you iterate over an iterable of values, use enumerate
to filter
values = ["dog0", "dog1", "dog2", "dog3", "dog4", "dog5", "dog6"]
for i, value in enumerate(values):
if i % 3 != 0:
continue
print(i, value)
You can do something like this
arr = [1,2,3,4,5,6,7,8,9,10]
skip_count = 2 # how many to skip
counter = skip_count
for num in arr:
if counter == skip_count:
counter = 0
print(num)
else:
counter += 1
Alternatively, this will work too
arr = [1,2,3,4,5,6,7,8,9,10]
for index in range(0, len(arr), 3):
print(arr[index])
One possible approach would be using enumerate
and mod (%
).
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8]
for index, num in enumerate(nums):
print(num) if index % 3 == 0 else None
If it’s a range you can use the optional step argument range(start, stop, step)
for k in range(start, stop, 3): print(k)
or
for k in range(len(my_data), step=3): print(k)
If it’s a list you can use list slicing
where once again the optional arguments for slicing is start, stop and step!
for k in my_array[::3]: print(k)
I would use enumerate
as others have mentioned. Your problem basically consists in printing all positions [i]
that are multipes of 3
. You can use i%3
to detect when the iteration counter i
is not a multiple of 3:
for i,x in enumerate(???):
if i%3: continue
print(x)
enumerate
also works when you have an iterator f
of unkown size (like when you read a file, etc.)