a quickie: range - x

R

rjtucke

I want an iterable from 0 to N except for element m (<=M).
I could write
x = range(N)
x.remove(m)
but I want it in one expression.

Thanks,
Ross
 
P

Paul Rubin

rjtucke said:
I want an iterable from 0 to N except for element m (<=M).
I could write
x = range(N)
x.remove(m)
but I want it in one expression.

itertools.chain(xrange(0,m), xrange(m+1, N))
 
S

Steven D'Aprano

I want an iterable from 0 to N except for element m (<=M).
I could write
x = range(N)
x.remove(m)
but I want it in one expression.

Because the world will end if you can't?

What's wrong with the solution you already have? If you need to use it
many times, make it a function. Calling the function is always a one-liner.

Here's another solution:

x = [i for i in range(N) if i != m]

Here's another:

x = range(m-1) + range(m+1, N)

Here's a generator to do it:

def range_except(N, m):
"""Iterator that returns ints from 0 through N-1 except for m."""
for i in xrange(N):
if i != m:
yield i
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top