A Tuple with One Element Requires a Comma in Python
Tuples are immutable sequences in Python. Be cautious when creating a tuple with one element (single-element tuple) or an empty tuple.
Tuple with one element
When attempting to create a tuple with one element (single-element tuple), simply enclosing an object in parentheses ()
is insufficient. The parentheses ()
are ignored, and the object is not treated as a tuple.
single_tuple_error = (10)
print(single_tuple_error)
# 10
print(type(single_tuple_error))
# <class 'int'>
To create a tuple with one element, a comma ,
is required at the end.
single_tuple = (10, )
print(single_tuple)
# (10,)
print(type(single_tuple))
# <class 'tuple'>
To access an element, even if there is only one element in the tuple, you can use the index notation []
.
print(single_tuple[0])
# 10
For example, when concatenating multiple tuples using the +
operator, be aware that an error will be raised if you attempt to add a single element without a comma ,
.
# print((0, 1, 2) + (3))
# TypeError: can only concatenate tuple (not "int") to tuple
print((0, 1, 2) + (3, ))
# (0, 1, 2, 3)
Tuple parentheses are basically optional
The reason you need a comma ,
for a tuple with one element is that a tuple is defined as an object delimited by commas ,
, rather than as an object enclosed in parentheses ()
.
Note that it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity.
Built-in Types - Tuples — Python 3.11.3 documentation
Even if the parentheses ()
are omitted, the object is still considered a tuple.
t = 0, 1, 2
print(t)
# (0, 1, 2)
print(type(t))
# <class 'tuple'>
Note that if there is an unnecessary comma ,
after the object, it will be treated as a tuple.
t = 0,
print(t)
# (0,)
print(type(t))
# <class 'tuple'>
Empty tuple
As mentioned above, parentheses ()
can be omitted to represent a tuple but are required when creating an empty tuple.
SyntaxError
is raised if there is only a space or a comma ,
.
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
An empty tuple can also be created by calling tuple()
without any arguments.
empty_tuple = tuple()
print(empty_tuple)
# ()
print(type(empty_tuple))
# <class 'tuple'>
Tuples in function arguments
In cases of syntactic ambiguity, tuples require parentheses ()
to be clearly distinguished.
Multiple arguments are specified by separating them with a comma ,
. If you want to specify a tuple as one argument, parentheses ()
are required.
Without parentheses ()
, each value is passed to each argument, while with parentheses ()
, a tuple is passed as 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 you add an asterisk *
to the tuple, you can unpack the tuple and pass each element to each argument.
example(*(0, 1))
# 0 <class 'int'>
# 1 <class 'int'>