By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

a quickie: range - x

P: n/a
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

Nov 30 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
(x for x in xrange(N+1) if x != m)

rjtucke wrote:
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
Nov 30 '06 #2

P: n/a
On Wed, 29 Nov 2006 19:42:16 -0800, rjtucke wrote:
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


--
Steven.

Nov 30 '06 #3

P: n/a
"rjtucke" <rj*****@gmail.comwrites:
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))
Nov 30 '06 #4

P: n/a
Thanks, all- that helped.
Ross

Nov 30 '06 #5

P: n/a
Steven D'Aprano wrote:
On Wed, 29 Nov 2006 19:42:16 -0800, rjtucke wrote:
>I want an iterable from 0 to N except for element m (<=M).
x = range(m-1) + range(m+1, N)
Should be range(m) + range(m+1, N)

Kent
Nov 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.