[Python-ideas] Syntax idea for 2D lists\arrays

Steven D'Aprano steve at pearwood.info
Thu Mar 15 01:15:26 EDT 2018


On Thu, Mar 15, 2018 at 01:32:35AM +0100, Mikhail V wrote:

> Idea is a concept for 2D arrays/lists syntax, which should simplify
> some editing boilerplate while working with arrays and improve
> readability for bigger arrays.

I don't understand; we already have perfectly good syntax for working 
with 2D arrays.

> Lets start with a simple list example :
> 
> L ===
>     1   5   9   155
>     53  44  44  34
> 
> returns a 2d list:
> [[1, 5, 9, 155], [53, 44, 44, 34]]

We already have:

L = [[1, 5, 9, 155], [53, 44, 44, 34]]

which is more compact (one line rather than two) and explicitly delimits 
the start and end of each list. Like everything else in Python, it uses 
commas to separate items, not whitespace. If you prefer:

L = [[1, 5, 9, 155], 
     [53, 44, 44, 34]]


Using spaces to separate items has the fatal flaw that it cannot 
distinguish 

    x - y 0  # two items, the expression `x - y` and the integer 0

from:

   x - y 0  # three items, `x`, `-y`, and 0


making it ambiguous. I stopped reading your post once I realised that.


-- 
Steve


More information about the Python-ideas mailing list