Why are trailing commas allowed in a list?

trailing comma python
trailing comma meaning
trailing comma best practice
trailing-comma tslint
use trailing comma in multi-line initializers
trailing comma prettier
trailing comma json
trailing comma php

I am curious why in Python a trailing comma in a list is valid syntax, and it seems that Python simply ignores it:

>>> ['a','b',]
['a', 'b']

It makes sense when its a tuple since ('a') and ('a',) are two different things, but in lists?


The main advantages are that it makes multi-line lists easier to edit and that it reduces clutter in diffs.

Changing:

s = ['manny',
     'mo',
     'jack',
]

to:

s = ['manny',
     'mo',
     'jack',
     'roger',
]

involves only a one-line change in the diff:

  s = ['manny',
       'mo',
       'jack',
+      'roger',
  ]

This beats the more confusing multi-line diff when the trailing comma was omitted:

  s = ['manny',
       'mo',
-      'jack'
+      'jack',
+      'roger'
  ]

The latter diff makes it harder to see that only one line was added and that the other line didn't change content.

It also reduces the risk of doing this:

s = ['manny',
     'mo',
     'jack'
     'roger'  # Added this line, but forgot to add a comma on the previous line
]

and triggering implicit string literal concatenation, producing s = ['manny', 'mo', 'jackroger'] instead of the intended result.

Trailing commas, ECMAScript 2017 allows trailing commas in function parameter lists. Parameter definitions. The following function definition pairs are legal and  Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. If you want to add a new property, you can simply add a new line without modifying the previously last line if that line already uses a trailing comma.


It's a common syntactical convention to allow trailing commas in an array, languages like C and Java allow it, and Python seems to have adopted this convention for its list data structure. It's particularly useful when generating code for populating a list: just generate a sequence of elements and commas, no need to consider the last one as a special case that shouldn't have a comma at the end.

Learn JavaScript ES 2017, at the end of a series of values in an array or array like object, leaving an essentially empty slot. Why are trailing commas allowed in a list? I am curious why in Python a trailing comma in a list is valid syntax, and it seems that Python simply ignores it:


It helps to eliminate a certain kind of bug. It's sometimes clearer to write lists on multiple lines. But in, later maintenace you may want to rearrange the items.

l1 = [
        1,
        2,
        3,
        4,
        5
]

# Now you want to rearrange

l1 = [
        1,
        2,
        3,
        5
        4,
]

# Now you have an error

But if you allow trailing commas, and use them, you can easily rearrange the lines without introducing an error.

Can you use a trailing comma in a JSON object?, A trailing comma is simply a comma that comes after the last item in our list. It's not required, but in some instances in JS we're allowed to put it  JavaScript allowed trailing commas in array literals since the beginning. Trailing commas in other places have added in the later edition of ECMAScript. What is trailing comma & why? Trailing commas is also called as final commas. It can be used while if you are adding new parameters or property to JavaScript code.


A tuple is different because ('a') is expanded using implicit continuation and ()s as a precendence operator, whereas ('a',) refers to a length 1 tuple.

Your original example would have been tuple('a')

trailing commas, I am curious why in Python a trailing comma in a list is valid syntax, and it seems that Python simply ignores it: >>> ['a','b',] ['a', 'b']. It makes sense when its a  This RFC proposes allowing trailing commas in all list syntax in order to: Offer a consistent API and match the existing array functionality. Make it easy to append new elements to a list in user-land. Have cleaner diffs (so only one line shows up as being modified when appending elements instead of two).


The main reason is to make diff less complicated. For example you have a list :

list = [
    'a',
    'b',
    'c'
]

and you want to add another element to it. Then you will be end up doing this:

list = [
    'a',
    'b',
    'c',
    'd'
]

thus, diff will show that two lines have been changed, first adding ',' in line with 'c' and adding 'd' at last line.

So, python allows trailing ',' in last element of list, to prevent extra diff which can cause confusion.

python - Why are trailing commas allowed in a list?, Rule: trailing-comma. Requires or disallows trailing commas in array and object literals, destructuring assignments, function typings, named imports and exports  C89 doesn't allow the trailing comma. C99 does allow it. C++11 does allow it (7.2/1 has the grammar for an enum declaration). In fact C++ isn't entirely backward-compatible even with C89, although this is the kind of thing that if had it been in C89, you'd expect C++ to permit it.


trailing-comma, The trailing comma in PHP arrays has been legal forever (I am not sure when they 2017 trailing commas in function parameter lists are legal: It is the commas, not the parentheses, which are significant. The Python tutorial says: A tuple consists of a number of values separated by commas. Parentheses are used for disambiguation in other places where commas are used, for example, enabling you to nest or enter a tuple as part of an argument list.


PHP 7.3: Trailing Commas in Function Calls, Trailing commas refers to a comma at the end of a series of values in an array or . is that you will see trailing commas in subsetting procedures of lists, One blogger provides an override to allow trailing commas though I'd  I'm wondering why no-one pointed out that you can actually split the loop at 0th iteration and use leading comma instead of trailing one to get rid of the comparison code smell and any actual performance overhead in the loop, resulting in a code that's actually shorter, simpler and faster (due to no branching/conditionals in the loop) than


rfc:list-syntax-trailing-commas, The following lists would allow trailing commas: Grouped namepaces. Function/​method arguments (declarations & calls). Interface  Trailing comma I believe is allowed for backward compatibility reasons. There is a lot of existing code, primarily auto-generated, which puts a trailing comma. It makes it easier to write a loop without special condition at the end. e.g.