4

I've noticed that when I'm using python, I'll occasionally make a typographical error and have a definition that looks something like

L = [1,2,3,]

My question is, why doesn't this cause an error?

4
  • 2
    Why should it cause an error? What's wrong with the extra ","? Why should that be an error? Commented Feb 22, 2012 at 21:14
  • 2
    I figured it would mean there is something else to the list. Kind of like having an open parenthesis. Commented Feb 22, 2012 at 21:23
  • Except, it's not like an open (). Because () come in pairs. , doesn't come in pairs. Commented Feb 22, 2012 at 21:30
  • 1
    It is in fact often seen as good practice and style to add trailing commas if you split the list/tuple into several lines. Commented Feb 23, 2012 at 13:52

5 Answers 5

8

It doesn't cause an error because it is an intentional feature that trailing commas are allowed for lists and tuples.

This is especially important for tuples, because otherwise it would be difficult to define a single element tuple:

>>> (100,)   # this is a tuple because of the trailing comma
(100,)
>>> (100)    # this is just the value 100
100

It can also make it easier to reorder or add elements to long lists.

Sign up to request clarification or add additional context in comments.

3 Comments

Also, it makes machine-generated code easier (you don't have to add the special case for the trailing comma).
No need to special-case the trailing comma if you're doing ",".join(elements). :-)
@kindall: But ",".join(elements) won't make a tuple if len(elements) is 1.
4

From the Python docs:

The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases. A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: ().)

Comments

4

My question is, why doesn't this cause an error?

The trailing comma is ignored because it can be convenient:

funcs = [ run,
          jump,
          # laugh
        ]

Comments

3

You can read more about in the official documentation:

Why does Python allow commas at the end of lists and tuples?

Python lets you 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],  # last trailing comma is optional but good style
}

There are several reasons to allow this.

When you have a literal value for a list, tuple, or dictionary spread across multiple lines, it’s easier to add more elements because you don’t have to remember to add a comma to the previous line. The lines can also be sorted in your editor without creating a syntax error.

Accidentally omitting the comma can lead to errors that are hard to diagnose. For example:

x = [
  "fee",
  "fie"
  "foo",
  "fum"
]

This list looks like it has four elements, but it actually contains three: "fee", "fiefoo" and "fum". Always adding the comma avoids this source of error.

Allowing the trailing comma may also make programmatic code generation easier.

Comments

1

Do this

>>> l = 1,2,3,
>>> l
(1, 2, 3)

The ()'s are optional. The ,, means that you're creating a sequence.

Observe this

>>> l = 1,
>>> l
(1,)
>>> l = 1
>>> l
1

Again. The , means it's a sequence. The () are optional.

It's not wrong to think of

[ 1, 2, 3, ]

as a tuple 1, 2, 3, inside a list constructor [ ]. A list is created from the underlying tuple.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.