I want to create a for loop that will go through the integers 0 to k-1, except for integer i. (I'm comparing some lists of k items, and I don't need to compare item i in one list with item i in another list.)
I have a fairly easy way to do it, but I keep thinking there's a more "Pythonic", elegant way to do it.
What I'm doing is:
tocheck = range(k)
del(tocheck[i])
for j in tocheck:
It's easy enough, but one thing I like about Python is that it seems like there's always a clever one-line "Pythonic" trick for things like this.
Thanks.
Perhaps using itertools.chain
from itertools import chain
for j in chain(range(i), range(i+1, k)):
# ...
I think the most idiomatic way to leave a gap would be to skip in the for loop using continue
.
i = 20
for j in range(50):
if j==i:
continue
print j
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With