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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The main advantages are that it makes multi-line lists easier to edit and that it reduces clutter in diffs.
Changing:
to:
involves only a one-line change in the diff:
This beats the more confusing multi-line diff when the trailing comma was omitted:
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:
and triggering implicit string literal concatenation, producing
s = ['manny', 'mo', 'jackroger']instead of the intended result.