Tuples with a single element in Python require a trailing comma

Money and Business

Tuples, which are immutable (unchangeable) sequence objects in Python.

Care must be taken when generating tuples with a single element or empty tuples.

The following details are described here.

  • Tuple with 1 element
  • Tuple round brackets can be omitted.
  • Empty tuple
  • Tuples in function arguments

Tuple with 1 element

If you try to generate a tuple with one element and write only one object inside the round brackets (), the round brackets () will be ignored and processed and not considered a tuple.

single_tuple_error = (0)

print(single_tuple_error)
print(type(single_tuple_error))
# 0
# <class 'int'>

A trailing comma is required to generate a tuple with one element.

single_tuple = (0, )

print(single_tuple)
print(type(single_tuple))
# (0,)
# <class 'tuple'>

For example, when using the + operator to concatenate multiple tuples, note that if you try to add one element and forget a comma, you will get an error.

# print((0, 1, 2) + (3))
# TypeError: can only concatenate tuple (not "int") to tuple

print((0, 1, 2) + (3, ))
# (0, 1, 2, 3)

Tuple round brackets can be omitted.

The reason why a tuple with one element needs a comma is because a tuple is not a value enclosed in round brackets () but a value separated by a comma.

It is the comma that creates the tuple, not the round brackets.
Tuples — Built-in Types — Python 3.10.4 Documentation

Even if the round brackets () are omitted, it is processed as a tuple.

t = 0, 1, 2

print(t)
print(type(t))
# (0, 1, 2)
# <class 'tuple'>

Note that an unnecessary comma after an object is considered a tuple.

t_ = 0,

print(t_)
print(type(t_))
# (0,)
# <class 'tuple'>

Empty tuple

As mentioned above, the round brackets () can be omitted when representing a tuple, but are required when generating an empty tuple.

A space or comma alone will result in a SyntaxError.

empty_tuple = ()

print(empty_tuple)
print(type(empty_tuple))
# ()
# <class 'tuple'>

# empty_tuple_error = 
# SyntaxError: invalid syntax

# empty_tuple_error = ,
# SyntaxError: invalid syntax

# empty_tuple_error = (,)
# SyntaxError: invalid syntax

Empty tuples can also be generated by tuple() with no arguments.

empty_tuple = tuple()

print(empty_tuple)
print(type(empty_tuple))
# ()
# <class 'tuple'>

Tuples in function arguments

Tuple round brackets () are required even when there is a syntactic ambiguity.

Function arguments are separated by commas, but in this case, it is necessary to explicitly indicate whether the function is a tuple or not by the presence or absence of round brackets ().

Without parentheses (), each value is passed to each argument; with parentheses (), each value is passed as a tuple to one argument.

def example(a, b):
    print(a, type(a))
    print(b, type(b))

example(0, 1)
# 0 <class 'int'>
# 1 <class 'int'>

# example((0, 1))
# TypeError: example() missing 1 required positional argument: 'b'

example((0, 1), 2)
# (0, 1) <class 'tuple'>
# 2 <class 'int'>

If the tuple is marked with an asterisk *, the elements of the tuple can be expanded and passed as arguments.

example(*(0, 1))
# 0 <class 'int'>
# 1 <class 'int'>

For more information, see the following article.

Copied title and URL