Skip over a value in the range function in python
149,094
Solution 1
You can use any of these:
# Create a range that does not contain 50
for i in [x for x in xrange(100) if x != 50]:
print i
# Create 2 ranges [0,49] and [51, 100] (Python 2)
for i in range(50) + range(51, 100):
print i
# Create a iterator and skip 50
xr = iter(xrange(100))
for i in xr:
print i
if i == 49:
next(xr)
# Simply continue in the loop if the number is 50
for i in range(100):
if i == 50:
continue
print i
Solution 2
In addition to the Python 2 approach here are the equivalents for Python 3:
# Create a range that does not contain 50
for i in [x for x in range(100) if x != 50]:
print(i)
# Create 2 ranges [0,49] and [51, 100]
from itertools import chain
concatenated = chain(range(50), range(51, 100))
for i in concatenated:
print(i)
# Create a iterator and skip 50
xr = iter(range(100))
for i in xr:
print(i)
if i == 49:
next(xr)
# Simply continue in the loop if the number is 50
for i in range(100):
if i == 50:
continue
print(i)
Ranges are lists in Python 2 and iterators in Python 3.
Solution 3
It is time inefficient to compare each number, needlessly leading to a linear complexity. Having said that, this approach avoids any inequality checks:
import itertools
m, n = 5, 10
for i in itertools.chain(range(m), range(m + 1, n)):
print(i) # skips m = 5
As an aside, you woudn't want to use (*range(m), *range(m + 1, n))
even though it works because it will expand the iterables into a tuple and this is memory inefficient.
Credit: comment by njzk2, answer by Locke
Solution 4
for i in range(0, 101):
if i != 50:
do sth
else:
pass
Author by
David
Updated on July 05, 2022Comments
-
David almost 2 years
What is the pythonic way of looping through a range of numbers and skipping over one value? For example, the range is from 0 to 100 and I would like to skip 50.
Edit: Here's the code that I'm using
for i in range(0, len(list)): x= listRow(list, i) for j in range (#0 to len(list) not including x#) ...
-
aestrivex almost 10 yearsThe third suggestion throws TypeError because you did not explicitly create an iterator
-
Daniel almost 10 years(1) creates two lists, (2) concatenates two lists, (3) does not work, xrange is not a iterator. (4) should use xrange to avoid creating a list, and is the so far best solution.
-
aestrivex almost 10 yearsHow does (1) create two lists?
xrange(100)
is not a list. And you can avoid creating the second list by returning a generator instead:for i in (x for x in xrange(100) if x is not 50)
-
David almost 10 years#2 is perfect. Thanks
-
njzk2 almost 10 years@Daniel: 1/ creates one list only because of xrange. 2/ I know, not the most efficient, but clear. 3/ typo, thanks for pointing out 4/ probably, yes. (but for a 100 elements it is fine)
-
loxosceles about 7 years#2 with Python 3.x:
...list(range(50)) + list(range(51, 100)):
-
njzk2 about 7 years@loxosceles or using
itertools.chain(range(50), range(51, 100))
instead, to avoid creating lists (although my initial solution is indeed creating 2 lists) -
Asclepius over 4 years#2 doesn't work in Python 3. The rest are all quite inefficient.
-
njzk2 over 4 years@Acumenus this question is tagged
python
, notpython3
. If you have more efficient solutions, please share them so everyone can benefit :)