You can find out more about this in the official documentation:
Why does Python allow commas at the end of lists and tuples?
Python allows you to add a trailing comma at the end of lists , tuples, and dictionaries :
[1, 2, 3,] ('a', 'b', 'c',) d = { "A": [1, 5], "B": [6, 7],
There are several reasons to do this.
When you have a literal value for a list, tuple or dictionary distributed over several lines, it is easier to add more elements because you do not need to remember to add a comma to the previous line. Lines can also be sorted in your editor without creating a syntax error.
Accidental lack of a comma can lead to errors that are difficult to diagnose. For instance:
x = [ "fee", "fie" "foo", "fum" ]
This list looks like four items, but in fact it contains three: "fee" , "fiefoo" and "fum" . Always adding a comma avoids this source of errors.
Providing a trailing comma can also facilitate the creation of program codes.
source share