Iterate Through a List by Skipping Every 5Th Element

iterate through a list by skipping every 5th element

[newList[each] for each in range(len(newList)) if each % 5 != 4]

i am using python3 though, try if it works for you too.

as other answers suggested, using enumeration results better:

[each for index, each in enumerate(newList) if index % 5 != 4]

Skipping every other element after the first

def altElement(a):
return a[::2]

Iterate over every nth element in string in loop - python

If you want to do something every nth step, and something else for other cases, you could use enumerate to get the index, and use modulus:

sample = "This is a string"
n = 3 # I want to iterate over every third element
for i,x in enumerate(sample):
if i % n == 0:
print("do something with x "+x)
else:
print("do something else with x "+x)

Note that it doesn't start at 1 but 0. Add an offset to i if you want something else.

To iterate on every nth element only, the best way is to use itertools.islice to avoid creating a "hard" string just to iterate on it:

import itertools
for s in itertools.islice(sample,None,None,n):
print(s)

result:

T
s
s

r
g

skip every nth value when looping over a range - python

One such way to do that is just skip the iterations of the loop you don't want to iterate with a continue statement

for i in range(0,11):
if i % 3 == 0 and i != 0:
continue
print(i)


1
2
4
5
7
8
10

skip items while iterating over list

Yes sure but you should name your python list something else than list since list is a keyword in python :

row_to_skip=1

for i in range (len (my_list)):
item=my_list[i]
if (i==row_to_skip):
continue
print(item.activity)

Why does removing items from list while iterating over it skip every other item

What happens here is the following:

You start with the following list:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Then, in the first iteration of the for loop, you get the first item of the list which is 0, and remove it from the list. The updated list now looks like:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Then, at the second iteration of the loop, you get the second item in the updated list. Here, that second item is not 1 anymore but it is 2 (because you look at the updated list), so you remove 2 from the list. The updated list now looks like:
[1, 3, 4, 5, 6, 7, 8, 9]

And it goes on... until you get:
[1, 3, 5, 7, 9]

For Loop is skipping every other line while searching ArrayList

Using an Iterator as suggested by @nlloyd is a better solution here.

However, there might be an occasion where (for whatever reason) you can't use an Iterator (e.g. you are working in a language or library that doesn't provide iterators). The simple solution is to iterate in reverse:

for ( int i = possibles.size()-1; i >= 0; i--) {

It isn't clear why you need to add items to the list in the first place if you are going to remove them again immediately:

 ArrayList<Integer> possibles = new ArrayList <Integer>();
int guess = 50; //my guess is generated by another method. Not listed here
int tens = guess / 10 % 10;

for (int x = 10; x < 100; x++) {
if (x / 10 % 10 != tens) {
possibles.add(x);
}
}


Related Topics



Leave a reply



Submit