[Python] Does the extra comma at the end of a dictionary, list or set has any special meaning...

Discussão em 'Python' iniciado por Stack, Agosto 21, 2025.

  1. Stack

    Stack Membro Participativo

    I noticed by chance that adding an extra separator comma at the end of a list, dictionary or set is syntactically correct and does not seem to add anything to the data structure:

    In [1]: d1 = {'a': 1, 'b': 2}

    In [2]: d2 = {'c': 10, 'd': 20,}

    In [3]: d1
    Out[3]: {'a': 1, 'b': 2}

    In [4]: d2
    Out[4]: {'c': 10, 'd': 20}


    Does it have any special meaning or usage?
    The only one I found is to explicit a data structure during an initialization:

    In [14]: r = (1)

    In [15]: r
    Out[15]: 1

    In [16]: r = (1,)

    In [17]: r
    Out[17]: (1,)

    Continue reading...

Compartilhe esta Página