This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: super-Matlab-style ranged list literal initialization
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.11
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: serhiy.storchaka, steven.daprano, xuancong84
Priority: normal Keywords:

Created on 2021-08-17 03:11 by xuancong84, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg399707 - (view) Author: wang xuancong (xuancong84) Date: 2021-08-17 03:11
Different from Python 2, Python 3 has removed the capability to create a list from a range. In Python 2, we can use range(1,100,2) to create a list [1, 3, 5, ..., 99], but in Python 3, we can only use list(range(1,100,2)) or [*range(1,100,2)] where the latter is even slower. I would like to propose to use something like [1:100:2] to initialize a list, moreover, you can use [1:100:2, 1000:1200:5, 5000:6000, :10] to create a list of multiple segments of ranges, i.e., [1,3,5,...,99,1000,1005,1010,...,1195,5000,5001,5002,...,5999,0,1,2,...,9]. Ranged list creation is quite useful and is often used in multi-thread/multi-processing scheduling or tracked sorting. This is especially useful in deep learning where you want to shuffle the training data but keep track of their corresponding labels. In deep RNN, where every training instance has a different length, after shuffling/sorting, you also need to keep track of their corresponding lengths information and etc. Thanks!
msg399708 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-08-17 04:15
> Python 3 has removed the capability to create a list from a range.

That's incorrect. You can do `list(range(1,100,2))`. But generally, why create a list? range objects support many of the list APIs:

- iteration
- fast containment tests `x in range(1, 100, 2)`
- indexing (subscripts) and slicing
- reversal using a slice
- count and index methods

Range objects support the full sequence API, so why do you need to convert to a list?

    >>> from import Sequence
    >>> isinstance(range(1, 100, 2), Sequence)

For the unusual or rare cares where you do need a list, why do you need special syntax? Just call the list constructor.
msg399709 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-08-17 04:17
As a new feature, the absolute earliest we could add this would be Python 3.11. As new syntax, it would need a PEP.
msg399713 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-17 07:10
I concur with Steven.

Before range() and enumerate() were introduced there were discussions about introducing dedicated syntax. But it was decided that builtin functions are more preferable. range() was improved in Python 3, so you do not need to waste memory and time for a temporary list if you do not need it. You can create a list only if you absolutely need a list, and you can do it with the list constructor.
msg399715 - (view) Author: wang xuancong (xuancong84) Date: 2021-08-17 07:18
Another lazy explanation not wanting to improve anything
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89093
2021-08-17 07:18:38xuancong84setmessages: + msg399715
2021-08-17 07:10:45serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg399713

resolution: rejected
stage: resolved
2021-08-17 04:17:37steven.dapranosetmessages: + msg399709
versions: + Python 3.11
2021-08-17 04:15:49steven.dapranosetnosy: + steven.daprano
messages: + msg399708
2021-08-17 03:11:23xuancong84create