Burhan Khalid
Burhan Khalid

Reputation: 174624

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:

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

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

Upvotes: 184

Views: 73516

Answers (6)

Abir Halder
Abir Halder

Reputation: 1

Makes it easier to put elements at the end.

For example,

a = [1,2,3]

Now you can mistakenly do

a = [1,2,3 4] #SyntaxError

By putting trailing commas:

  1. You eliminate this issue in first go.
  2. Spare yourself from putting that comma everytime you add new elements.

Upvotes: 0

Raymond Hettinger
Raymond Hettinger

Reputation: 226296

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.

Upvotes: 312

Nitish Chauhan
Nitish Chauhan

Reputation: 385

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.

Upvotes: 4

Óscar López
Óscar López

Reputation: 236004

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.

Upvotes: 42

richo
richo

Reputation: 8989

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')

Upvotes: 6

Keith
Keith

Reputation: 43024

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.

Upvotes: 39

Related Questions