Search code examples
python-3.xconditional-operatorsquare-bracket

What is the [][] (double square brackets operator) operator in python?


I am new to python and I was looking for the correct way to do a ternary operation when assigning a variable.

I found a post on stack overflow of someone suggesting something like this:

var = ['smaller', 'bigger'][7 > 1]

where the The value on the left is False and the one on the right is True

I run it and it works. returns 'bigger'

But what is this syntax? Does it always work? Is it deprecated? I googled it but cound't find any reference in the python docs or anywhere else

Thank you


Solution

  • But what is this syntax?

    The first pair of square brackets is a list display. The second pair of square brackets is a slicing.

    Does it always work?

    "Always" is a very long time. There is no way of telling what Python will look like in a million years.

    However, to my knowledge neither the syntax for list displays nor the syntax of slicings has ever changed in a backwards-incompatible manner in Python, at least in the simple, basic form in your code. It may have been extended, and some advanced forms may have been changed, but the basic form has always been the same.

    In fact, the basic form of list displays and slicings in Python is not only the same across all versions of Python, but even many other programming languages as well. Using square brackets for lists / arrays is almost universal in languages that are inspired by ALGOL, as is using square brackets for indexing / subscripting / slicing.

    Is it deprecated?

    There is no mention of deprecating list displays or the current list display syntax, slicings or the current slicings syntax in the Deprecated section of the What's New In Python 3.10 document nor in the currently under development 3.11.

    I also could not find any mention of deprecating list displays or the current list display syntax, slicings or the current slicings syntax in any of the Python Enhancement Proposals.

    So, the earliest that they could be deprecated would be in Python 3.12, which means the earliest they could be removed would be in Python 3.13. However, that is vanishingly unlikely, since it would break every single Python program ever written. It would be an even more breaking change than the transition from Python 2 to Python 3, which took twelve years.

    I googled it but cound't find any reference in the python docs or anywhere else

    You can find the full syntax of Python in the chapter Full Grammar Specification of the Python Language Reference.

    This is the syntax for list display:

    list:
        | '[' [star_named_expressions] ']' 
    

    and for slicing:

    primary:
        | primary '.' NAME 
        | primary genexp 
        | primary '(' [arguments] ')' 
        | primary '[' slices ']' 
        | atom
    
    slices:
        | slice !',' 
        | ','.slice+ [','] 
    slice:
        | [expression] ':' [expression] [':' [expression] ] 
        | named_expression