Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Python-ideas] range() manipulations

11 views
Skip to first unread message

arvind...@gmail.com

unread,
Aug 1, 2022, 8:53:34 AM8/1/22
to python...@python.org
Hello everyone. First time here, so please be forgiving of any missteps on my part.
This is an idea I have thought about for a while, and I believe it could be quite useful.
Would it be possible to enhance the standard range() type to allow for elementary operations on ranges?
For example :
range(10) - range(5) => range(5, 10)
range(3, 5, 2) + range(3, 6, 2) => range(3, 6, 2)

I believe this could be a fair first step into allowing for a whole range of mathematical operations in abstract algebra.
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/VH3HBAZEKYYBWPBZRXVM3P647JXTUC6K/
Code of Conduct: http://python.org/psf/codeofconduct/

Paul Moore

unread,
Aug 1, 2022, 9:22:38 AM8/1/22
to arvind...@gmail.com, Python-Ideas
On Mon, 1 Aug 2022 at 13:50, <arvind...@gmail.com> wrote:
Hello everyone. First time here, so please be forgiving of any missteps on my part.
This is an idea I have thought about for a while, and I believe it could be quite useful.
Would it be possible to enhance the standard range() type to allow for elementary operations on ranges?
For example :
range(10) - range(5) => range(5, 10)
range(3, 5, 2) + range(3, 6, 2) => range(3, 6, 2)

There are a lot of complex cases you'd need to consider. What would the value of range(3, 15, 2) + range(8, 12, 2) be? It's not a range in the sense of being describable as (start, end, step). And simply saying "that's not allowed" wouldn't really work, as it would be far too hard to work with if operations could fail unexpectedly like this. In reality, this feels more like you're after set algebra, which Python already has.

I believe this could be a fair first step into allowing for a whole range of mathematical operations in abstract algebra.

For cases where the number of elements in the range is not ridiculously large, simply converting to sets is probably sufficient:

>>> set(range(10)) - set(range(5)) == set(range(5, 10))
True
>>> set(range(3,5,2)) | set(range(3,6,2)) == set(range(3,6,2))
True

For sufficiently large ranges, there are bitset classes on PyPI that might be more memory efficient. Or if you're looking for some other types of operation (unbounded ranges, or specialised abstract algebra operations) a custom class (maybe even published on PyPI) would probably be a better approach than trying to get something that specialised added to the stdlib.

Paul

Christopher Barker

unread,
Aug 1, 2022, 2:41:18 PM8/1/22
to Paul Moore, Python-Ideas, arvind...@gmail.com
For cases where the number of elements in the range is not ridiculously large, simply converting to sets is probably sufficient:

Sets don’t preserve order, which may or may not be important— but as the IP didn’t provide a use case, I have no idea.

To the OP: this idea isn’t likely y  on be accepted into Python itself without some compelling use cases.

But it’s nifty idea -. A great candidate for a prototype library that you can use to work out the details and demonstrate its utility. 

Once you’ve done that, it may (or may not) make sense to propose an extension to tbe builtin range object.

-CHB


>>> set(range(10)) - set(range(5)) == set(range(5, 10))
True
>>> set(range(3,5,2)) | set(range(3,6,2)) == set(range(3,6,2))
True

For sufficiently large ranges, there are bitset classes on PyPI that might be more memory efficient. Or if you're looking for some other types of operation (unbounded ranges, or specialised abstract algebra operations) a custom class (maybe even published on PyPI) would probably be a better approach than trying to get something that specialised added to the stdlib.

Paul
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython

arvind...@gmail.com

unread,
Aug 1, 2022, 2:52:34 PM8/1/22
to python...@python.org
Thanks so much Christopher. I have several ideas for use cases, but you're right, it's probably better off as a library. That's exactly what I'll do now.
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/UYAGWDHBJ3W6AWLWIRHAZCSH7BWJFEDJ/

arvind...@gmail.com

unread,
Aug 1, 2022, 2:54:23 PM8/1/22
to python...@python.org
You're right, sets does make sense in these specific use cases, but I want to preserve order too. You're also right, a custom class is a much better idea. Thanks so much.
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/6ZCC2C35BAT26QDFQJIPP7DLU7VR5BR6/

Oscar Benjamin

unread,
Aug 1, 2022, 5:39:22 PM8/1/22
to python-ideas
On Mon, 1 Aug 2022 at 13:50, <arvind...@gmail.com> wrote:
>
> Hello everyone. First time here, so please be forgiving of any missteps on my part.
> This is an idea I have thought about for a while, and I believe it could be quite useful.
> Would it be possible to enhance the standard range() type to allow for elementary operations on ranges?
> For example :
> range(10) - range(5) => range(5, 10)
> range(3, 5, 2) + range(3, 6, 2) => range(3, 6, 2)

SymPy has a symbolic Range object that interacts with its other Set types:

In [9]: from sympy import *

In [10]: Range(10) - Range(5)
Out[10]: {0, 1, …, 9} \ {0, 1, …, 4}

In [11]: list(_)
Out[11]: [5, 6, 7, 8, 9]

In [12]: Range(3, 5, 2) + Range(3, 6, 2)
Out[12]: {3} ∪ {3, 5}

That last example could be simplified but I guess the simplification
code for it has not been added.
https://docs.sympy.org/latest/modules/sets.html#range

The Range type is a bit weird because other SymPy Sets are not
considered to be ordered but Range is. Some operations will not
preserve the ordering because it isn't really well defined in general
set arithmetic:

In [35]: Range(10)
Out[35]: {0, 1, …, 9}

In [36]: Range(10)[::-1]
Out[36]: {9, 8, …, 0}

In [37]: Range(10) & Range(10)[::-1]
Out[37]: {0, 1, …, 9}

In [38]: Range(10)[::-1] & Range(10)
Out[38]: {0, 1, …, 9}

> I believe this could be a fair first step into allowing for a whole range of mathematical operations in abstract algebra.

Take a look at SymPy's sets module. There are various other kinds of
sets and things such as:

In [26]: Range(2) * Range(3)
Out[26]: {0, 1} × {0, 1, 2}

In [27]: list(_)
Out[27]: [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2)]

In [28]: Range(10) - {3}
Out[28]: {0, 1, …, 9} \ {3}

I don't think it makes sense to make any changes to the builtin range
objects for these kinds of operations.

--
Oscar
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/NTFZ7NTRP7MCPMWZ54EIV6CFGHKYEXUJ/

Random832

unread,
Aug 3, 2022, 3:27:50 PM8/3/22
to Python-ideas Python-ideas
On Mon, Aug 1, 2022, at 09:19, Paul Moore wrote:
> There are a lot of complex cases you'd need to consider. What would the
> value of range(3, 15, 2) + range(8, 12, 2) be? It's not a range in the
> sense of being describable as (start, end, step). And simply saying
> "that's not allowed" wouldn't really work, as it would be far too hard
> to work with if operations could fail unexpectedly like this. In
> reality, this feels more like you're after set algebra, which Python
> already has.

Maybe it would make sense for the type of the result to devolve into a sorted set (do we have a sorted set type now?) if it's not representable as a range.
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/PKGCHWYKC5P27FTKZH5LGVQ5RCZMMXUS/

Benedict Verhegghe

unread,
Aug 3, 2022, 4:19:45 PM8/3/22
to python...@python.org
These kind of problems can easily be handled with numpy:

>>> import numpy as np
>>> print(np.union1d(np.arange(3,15,2), np.arange(8,12,2)))
[ 3 5 7 8 9 10 11 13]


Op 3/08/2022 om 21:23 schreef Random832:
Message archived at https://mail.python.org/archives/list/python...@python.org/message/ZZQTL4M6RRFBMNFOQFT7Z3GNLKHKLLSB/
Reply all
Reply to author
Forward
0 new messages